假设您的html标记如下所示。。
<span class="xoo-cp-close xoo-cp-icon-cross">
<div class="title-bar">
<h1>Cheese</h1>
</div>
</span>
你可以像这样简单地迭代文档中的所有h1。。
jQuery(document).ready(function() {
jQuery(".title-bar h1", "span.xoo-cp-close.xoo-cp-icon-cross").each(function() {
var $h1 = jQuery(this),
h1Text = $h1.text().trim().toLowerCase();
if (h1Text.indexOf("cheese") > -1) {
$h1.attr("onClick", "window.location.href('abcx.com/cheese')");
}
});
});
上面的代码迭代所有
h1
在具有类的元素中
.title-bar
这是包含两个类的span元素的后代
xoo-cp-close
和
xoo-cp-icon-cross