0

以下のコードで何が間違っているのかわかりません。このフォーラムで見つけることができるほとんどすべてを試しましたが、成功しませんでした。H3タグ内の「a href」の値を取得しようとしています。

HTML

<div class="product" style="opacity: 1;">
   <a title="product" href="url-to-product.com">..................</a>
   <h3><a href="url-to-product.com">Blabla</a></h3>
</div>
<div class="product" style="opacity: 1;">
   <a title="product" href="other-url-to-product.com">..................</a>
   <h3><a href="other-url-to-product.com">Blabla</a></h3>
</div>

h3 タグ内の href から値を取得し、getJSON 関数で使用する変数にする必要があります。私は、最も近い、兄弟、そして次のようなことを試しました:

 $( ".opener" ).live("click", function(event) {
      event.preventDefault();
      $.get($(this).attr('href'), function(data, status) {
        $( "#dialog" ).dialog( "open" );

        **var url = $('.product h3 > a').attr('href')+'?format=json';**

        $.getJSON(url, function(data) {           

「未定義」エラーが常に発生します。何が間違っているのかわかりません。

4

4 に答える 4

1

使用しない理由:

$('h3 a').attr('href')

この問題を解決するには多くの方法がありますが、要件によって異なります。

于 2013-07-02T11:51:46.993 に答える
0

やったほうがいい:

 $('.product').find('h3 > a').attr('href')
于 2013-07-02T11:51:55.353 に答える
0

このようにしてみてください:

$('.product h3 > a').attr('href')

$(".product").each(function(){
    var a_href = $(this).find('h3 > a').attr('href');
    alert ("Href is: "+a_href);
});

フィドル

于 2013-07-02T11:55:19.773 に答える
0

h3 要素をターゲットにして、その 'href' 属性を取得しようとしています。あなたがしたいことは、要素内の要素をターゲットにして、その「href」属性を取得することです。

$(this).find("h3").children('a').attr('href');
于 2013-07-02T11:52:27.817 に答える