javascript - 在其他 JQuery 对象中查找 JQuery 对象

标签 javascript jquery html

抱歉我的英语不好

我有html

<noindex>
<h1>This is h1 in noindex</h1>
<div>
  <h1>This is h1 in noindex and in div</h1>
  <div>
  <h1>This is h1 in noindex and in div>div</h1>
  </div>
</div>
<h2>This is h2 in noindex</h2>
</noindex>

<h1>This is h1</h1>
<h2>This is h2</h2>

然后,在js中

var all_h1 = $('h1');
var all_h2 = $('h2');
all_h1.each(function()
{
  if($($(this),'<noindex>'))
  {
    //do somthing only with $(this)
  }
});

我知道,这不行。我使用 has()、find()、$.contains 等,但它不起作用。如何在其他jq.object中搜索某个jq.object?

最佳答案

你可以这样尝试

all_h1.each(function()
{
  if($(this).closest('noindex').length > 0)
  {
    //it is contained inside of <noindex>
  }
});

关于javascript - 在其他 JQuery 对象中查找 JQuery 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559304/

相关文章:

php - 如何将非子 div 附加到另一个 div 的底部

javascript - 非方形透明图标图像的悬停效果

javascript - 使用 Javascript 的两个 HTML 页面链接

javascript - 将 JSON 数据写入我的 HTML 文件

javascript - 如何将光标放在文本区域时弹出模式

javascript如何在连字符后使所有字符串变为斜体?

javascript - Highcharts 条形图数据标签重叠

jquery - 如何添加背景视频停止按钮

javascript - 如何在gulp中实现多源中流

javascript - Firebase 模拟器上的 Firebase 权限被拒绝错误