我只是想在页面上存在特定字符串时收到一条警告,提示“页面包含字符串”。
不应该像这样简单吗:
if ('*:contains("This String")') {
alert('Page Contains String');
}
最佳答案
原生 JS:
if(document.body.innerText.indexOf("This String") !== -1){
// Do stuff
}
您可能想使用document.body.textContent
相反,不过。取决于您想要/需要支持的浏览器。 (IE 8 或更低版本不支持 textContent
。)
为了获得最大兼容性,请尝试以下操作:
var text = document.body.innerText || document.body.textContent;
if(text.indexOf("This String") !== -1){
//Do stuff
}
关于javascript - 如果页面上存在字符串则执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23654718/