我有以下用 jQuery 撰寫的函式,我想將其轉換為 javascript,但到目前為止我找不到合適的方法。
const word = document.getElementById("searchField").value;
const r = new RegExp("(" word ")", "ig");
$(".list-item").each(function (i) {
if ($(this).text().match(r)) {
}
});
我是這樣重寫的:
const word = document.getElementById("searchField").value;
const r = new RegExp("(" word ")", "ig");
let pickComp = document.querySelectorAll('.list-item');
Array.from(pickComp).forEach((i) => {
if (//how can I rewrite the jQuery here?) {
}
})
uj5u.com熱心網友回復:
const word = document.getElementById("searchField").value;
const r = new RegExp("(" word ")", "ig");
const pickComp = document.querySelectorAll('.list-item');
pickComp.forEach(item => {
if (item.innerHTML.match(r)) {
console.log("Match!!");
}
})
<p class="list-item">abc</p>
<p class="list-item">abc</p>
<p class="list-item">abc</p>
<p class="list-item">abc</p>
<input id="searchField" value="abc">
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/372152.html
標籤:javascript 查询 功能 这个
