我正在過濾整個 DOM 以用其他字串替換一些字串。
為此,我撰寫了以下代碼:
$('body :not(script)').contents().filter(function() {
return this.nodeType === 3;
}).replaceWith(function() {
return this.nodeValue.replace('T 0','T 0');
});
如何排除某些區域?例如,我希望.example不考慮該類的 DIV 。
我嘗試了以下方法:
$('body :not(script):not(.example)').contents().filter(function() {
return this.nodeType === 3;
}).replaceWith(function() {
return this.nodeValue.replace('T 0','T 0');
});
但這似乎不起作用。為什么?
uj5u.com熱心網友回復:
現在,您告訴 jQuery 排除節點.example,它有子節點,將被包括在內。
在您的:not()選擇器中*,在類之后添加一個。這將告訴 jQuery 排除父節點下的所有節點。
在 CSS 中,這*是一個通用選擇器
CSS 通用選擇器 (*) 匹配任何型別的元素。
這是一個作業版本:(請注意,我更改了 to-以直觀地顯示更改)
$( "input:not(:checked) span" ).css( "background-color", "yellow" );
$( "input").attr( "disabled", "disabled" );
$('body :not(script):not(div.example *)').contents().filter(function() {
return this.nodeType === 3;
}).replaceWith(function() {
return this.nodeValue.replace('T 0','T--0');
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>not demo</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="example">
<p>Remains the same:</p>
<h1>T 0</h1>
</div>
<div>
<p>Changes:</p>
<h1>T 0</h1>
</div>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/381483.html
標籤:javascript 查询
上一篇:帶角色的用戶注冊
