3 回答
TA贡献1877条经验 获得超1个赞
在我看来,最好的方法,就是在选择器中使用不
$('p:not(.js-ignore)') // ignore the section by class "js-ignore"
.css('background','blue') //whatever your code dose
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<p>process with javascript</p>
<p class='js-ignore'>skip, have javascript function ignore</p>
<p>process with javascript</p>
通过你的方式,我相信你可以做这样的事情:
$( "p" ).each(function() {
if(!$(this).parents('script-ignore').length){
$(this).css('background','blue')
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<p>process with javascript</p>
<script-ignore><p>skip, have javascript function ignore</p></script-ignore>
<p>process with javascript</p>
更新 1
我意识到你没有提到“jquery”,所以这是纯香草JS:
var p = document.querySelectorAll('p'); // select
for( i=0; i< p.length; i++ ){
if(p[i].parentElement.localName !== 'script-ignore'){
// whatever your code is
p[i].style.background = 'red'
p[i].style.color = '#fff'
}
}
<p>process with javascript</p>
<script-ignore><p>skip, have javascript function ignore</p></script-ignore>
<p>process with javascript</p>
TA贡献1860条经验 获得超9个赞
<p>process with javascript</p>
<p class="dontIncludeJavascriptCall">skip, have javascript function ignore</p>
<p>process with javascript</p>
var ps = document.querySelectorAll("p:not(.dontIncludeJavascriptCall)");
console.log(ps);
这只会给你2个没有该类的P标签
添加回答
举报