【问题标题】:Find element without class or id within element - jQuery在元素中查找没有类或 id 的元素 - jQuery
【发布时间】:2013-06-21 19:32:19
【问题描述】:
<td class="box">
<span>
<img src='http://something.com' />
</span>
</td>

如何选择 span 标签? 我需要使用 jQuery 添加额外的图像(用于用户脚本),但是选择跨度然后添加元素比我想象的要难。

【问题讨论】:

    标签: jquery element


    【解决方案1】:

    这些都应该有效

    $('span', '.box');
    $('.box span');
    $('.box > span');
    $('.box').find('span');
    $('.box').children('span');
    $('.box').children('span').first();
    

    等等。 ....

    甚至

    $('img[src$="something.com"]').before('<img src="newimage.jpg" />');
    $('span:has(img)');
    $('span').filter(function() { return $(this).find('img[src$="something.com"]').length })
    

    【讨论】:

    • 非常感谢。我试过了,但显然我做错了什么。
    【解决方案2】:
    $('img[src="http://something.com"]').closest('span');
    

    如果您只想定位具有特定src 的图像的特定span,则可以使用上述选择器。

    但是,如果您想将所有td.box 作为其后代的目标,那么 @adeneo 的建议应该有效。

    【讨论】:

    • 从我阅读问题的方式来看,我认为他需要找到span 才能添加图像。
    • @beauXjames.. 你能解释一下吗??
    【解决方案3】:
    $('.box').find('span');
    

    会成功的!

    【讨论】:

    • $('.box') :: 添加'.'资格类
    【解决方案4】:

    $('.box span') 会让你选择跨度

    【讨论】:

      【解决方案5】:

      可以使用jquery查找方法http://api.jquery.com/find/

      【讨论】:

        【解决方案6】:

        只有你展示的 sn-p,我可能会使用这样的东西:

        $('td.box span')
        

        如果 td.box 有其他父项,您可以使用它们来获得更具体的信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-21
          • 1970-01-01
          • 2014-08-14
          相关资源
          最近更新 更多