【问题标题】:jquery img selectjquery img 选择
【发布时间】:2010-01-07 20:36:50
【问题描述】:

我有一个像这样的带有 img 的 h4。我将点击功能绑定到 h4。这很好用。但我无法选择其中的 img。我想选择 img 以便用 .attr("src").replace("up", "down"); 替换 src attr .

<h4 class="collapsable_head">
<img id="up_down" class="icon" src="/crm/img/modifier_down.gif" alt="link"/>
<b>Classification Filter:</b>
</h4>

javascript:

$(".collapsable_head").click(function(){
   $(this).next(".collapsable_body").slideToggle(500)
   //None of the next lines return me the img object
   src = jQuery(this).children('img').attr("src");
   print(src);
   src = $(this).next("#up_down").attr("src");
   print(src);
   src = $(this).next("#up_down").attr("src");
   print(src);
   return false;
});

我想使用关键字“this”,因为我还有更多(“.collapsable_head”);-)

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:
    var img = $("img", this); // Gets the image within 'this'
    

    第二个参数是选择器的上下文。在完整代码中,它看起来像这样:

    $(".collapsable_head").click(function(){
      var img = $("img:first", this).attr("src");
      alert(img);
    });
    

    【讨论】:

    【解决方案2】:

    你试过了吗:

    jQuery(this).find('img').attr('src')
    

    应该和乔纳森的回答一样。

    【讨论】:

      【解决方案3】:
        $(function(){
            $("img").on('error', function() {
                $(this).attr('src',"/Files/446/178/themeblue/images/nopic.jpg");
            });   
       });  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-07
        • 1970-01-01
        • 2013-04-10
        • 2021-05-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多