[英]How to search text in html with JQUERY?
我試圖用 jquery 在我的 html 代碼中搜索文本,但沒有運氣,這是我的代碼:
$("body").on("searchByLikuy", function(e, msg) {
msg = msg.replace(/'/g, "\'");
found = $("*:contains('" + msg + "'):last");
if(found.length > 0) {
$('html, body').stop(true).animate({
scrollTop: found.offset().top - 30
}, 1000);
}
});
當html代碼為:
<html>
<head>
</head>
<body>
<div class="test">
abcde
</div>
</body>
</html>
我搜索味精“abcde”它的工作正常並在“找到”變量中返回我需要的東西。 但是當 html 代碼是:
<html>
<head>
</head>
<body>
<div class="test">
<span>a</span>
<span>b</span>
<span>c</span>
<span>d</span>
<span>e</span>
</div>
</body>
</html>
它不返回任何東西。 我可以做什么來搜索?
發件人
您可以簡單地嘗試:
if( $("body").text().includes(msg) ) { ...
或者使用正則表達式:
if( new RegExp(msg, "i").test( $("body").text() )) { ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.