将 $(this).text().match(r) 写入 vanilla JS 的正确方法?
Proper way of writing $(this).text().match(r) into vanilla JS?
我在 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?) {
}
})
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">
我在 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?) {
}
})
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">