【问题标题】:How to select the IMG tag based on the attribute?如何根据属性选择IMG标签?
【发布时间】:2011-02-17 14:47:37
【问题描述】:
<IMG height="160" alt="Web Content Image" src="../image/journal/article?img_id=16505&t=1273484793531" width="240" pngSet /> 

我有这个图像的代码,我想根据属性“pngSet”选择图像

我正在尝试这个,但不工作。

$("img[pngSet='']").css('border' ,'1px solid red');

【问题讨论】:

    标签: jquery jquery-selectors css-selectors


    【解决方案1】:

    你可以这样做:

     $("img[pngSet]").css('border' ,'1px solid red');
    

    has attribute selector 就是 [attributeName] :)

    【讨论】:

    • @wombleton - 虽然不能保证跨浏览器,但我不记得在哪些浏览器上失败了,但有一些,或者至少曾经是。
    【解决方案2】:

    应该是:

    $('img[pngSet]')
    

    【讨论】:

      【解决方案3】:
      $("img[pngSet]");
      

      【讨论】:

        【解决方案4】:

        组成属性通常是个坏主意。浏览器不必支持它。最好是改用一个类:

        <IMG height="160" alt="Web Content Image" src="../image/journal/article?img_id=16505&t=1273484793531" width="240" class="pngSet" /> 
        $("img.pngSet").css('border' ,'1px solid red');
        

        【讨论】:

          【解决方案5】:

          试一试(使用has attribute selector):

          $("img[pngSet]")
          

          【讨论】:

            猜你喜欢
            • 2017-09-25
            • 1970-01-01
            • 2011-09-20
            • 1970-01-01
            • 2013-11-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多