[英]Check if text exist in div using jquery
I would like to check if the text exist in the div element, so that if text matches the text in div it will alert "hello".我想检查文本是否存在于 div 元素中,这样如果文本与 div 中的文本匹配,它就会提醒“你好”。 May I know how am I able to achieve this result?
我可以知道我怎样才能达到这个结果吗? Thank you.
谢谢你。
<script type="text/javascript">
jQuery(document).ready(function(){
var text = "div[style*=\"width: 550px;\"]";
if (#content.indexOf(text) > -1){
alert("Hello");
}
});
</script>
<div id="content">
<div style="width:550px;">James</div>
<div style="width:500px;">Amy</div>
</div>
Here you go with a solution https://jsfiddle.net/9kLnvyqm/在这里,您可以使用解决方案https://jsfiddle.net/9kLnvyqm/
if($('#content').text().length > 0) { // Checking the text inside a div // Condition to check the text match if($('#content').text().indexOf('Amy')){ console.log('Hello'); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="content"> <div style="width:550px;">James</div> <div style="width:500px;">Amy</div> </div>
If you want only the text content from a container then use text()
, if you are looking for html content then use html()
.如果您只想要容器中的文本内容,则使用
text()
,如果您正在寻找 html 内容,则使用html()
。
Hope this will help you.希望这会帮助你。
It is possible to get the value of inline style of an element.可以获取元素的内联样式的值。
var wid = $("#content > div")[0].style.width; if(wid === "550px"){ //correct width detected. you can use alert instead of console.log console.log("hello"); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="content"> <div style="width:550px;">James</div> <div style="width:500px;">Amy</div> </div>
You have multiple elements inside #content. #content 中有多个元素。 you may want to use the return value of
您可能想要使用的返回值
$('#content').children().length;
and loop the program to get inline width of all elements.并循环程序以获取所有元素的行内宽度。 Ping if you need some help with the loop
如果您需要有关循环的帮助,请 Ping
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.