2012-03-07 6 views
0

この件ではたくさんの項目がありますが、私の質問には答えが見つかりませんでした...もしこれが不必要であれば、私を傷つける。私はこれで本当に頑張った!jQuery IFウィンドウはそれを配列と比較して一致する値をクリックしています

目的は簡単です。私はいくつかのdivがあり、その中にはアンカーが含まれています。私は、ウィンドウのハッシュをdivのアンカー子のタイトル属性と比較して、一致するアンカーの親をクリックしようとしています。

以下の関数は、正しいアンカー子の配列を記録しますが、クリック結果が表示されません。私は何かが欠けていると確信しています。

ありがとうございました。あなたがそこにやっているようにそれが見えるものから、

<div class="box"><img src="#" /></div> 
<div class="box"><a href="#" title="blah"><img src="#" /></a></div> 
<div class="box"><img src="#" /></div> 

if(window.location.hash) { 
    var hash = window.location.hash.substring(1), 
     box = $('.box').children('a').each(function(){ 
        $(this).attr('title'); 
       }); 

    if (box.attr('title') == hash) { 
     $(this).parent().click(); 
    } 
} 

答えて

1

$(this).attr('title')は何もしていない - それだけで値を返して、そして何がそれで行わされていません。

それは私だったら、私はこのようにそれを行うだろう:

if(window.location.hash) { 
    var hash = window.location.hash.substring(1), 
     boxes = []; 

    $('.box').children('a').each(function(){ 
     if ($(this).attr('title') == hash) { 
      boxes.push(this); // OR: 
      $(this).parents("div.box").click(); 
     } 
    }); 

    // Or, you can iterate through your boxes array and perform more validation here. 
} 
関連する問題