[英]jquery: check if string exist in element, return boolean
Assuming I have this:假设我有这个:
<div id="elementId">bla bla bla some text bla bla</div>
I want to check if this div text contains 'some text', then return true, otherwise return false, how to do that?我想检查这个 div 文本是否包含“一些文本”,然后返回 true,否则返回 false,怎么做?
var isContains = $('#elementId').text().indexOf('some text') > -1;
您可以使用:contains()
选择器并检查.length
以查看它是否匹配任何内容,如下所示:
return $("#elementId:contains('some text')").length > 0;
I used the same way of the buddy above...:我用了和上面那个哥们一样的方法...:
if($('#div_searched').text().indexOf('bla') != -1)
This will return true when it found the word bla on div_searched当它在 div_searched 上找到单词 bla 时,这将返回 true
let myNode = document.getElementById("elementId")
const Value = myNode.textContent || myNode.innerText
const txtExist = Boolean(Value.replace(/\s/g, ""));
Note: innerText does work in IE, also textContent is not supported in IE8 and older注意:innerText 在 IE 中确实有效,在 IE8 及更早版本中也不支持 textContent
I think this should also work.我认为这也应该有效。
return $("#elementId:contains('some text')").length ? true : false;
It should return true if length greater than 0, otherwise it will return false.如果长度大于 0,则返回 true,否则返回 false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.