[英]Keep checking If Div contains specific text
您好,所以我想继续检查一个函数,如果一个div包含特定文本,这是我到目前为止的内容,但是似乎没有任何作用?
JS
var interval = 500;
var timer = window.setInterval(function() {
var text = $('.text-split-original').text();
var checkText = '4) PLACEHOLDER '
if(text == checkText){
alert("It has this text");
//do something
};
}, interval);
的HTML
<div class="sar__label">
<div class="text-split-original">
4) PLACEHOLDER
<br>
</div>
</div>
在此先感谢您的帮助。
您只需要修剪空间就可以了。
var interval = 500; var timer = window.setInterval(function() { var text = $.trim($('.text-split-original').text()); var checkText = $.trim('4) PLACEHOLDER ') if(text == checkText){ alert("It has this text"); //do something }; }, interval);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <div class="sar__label"> <div class="text-split-original"> 4) PLACEHOLDER <br> </div> </div>
添加.trim()
将删除导致比较失败的空白:
var text = $('.text-split-original').text().trim();
另外, checkText
字符串'4) PLACEHOLDER '
在末尾包含一个额外的空格,这也会破坏比较。
除此之外,运行间隔不是执行事情的最有效方法。 也许您可能想看看div的内容何时更改 。 编辑 :不推荐使用,如@Barmar所指出,请参阅MutationObserver 。
在此处查看您的固定代码(我已注释掉间隔以防止自己发垃圾邮件): https : //codepen.io/anon/pen/JMRbMx
没有JQuery:
<html>
<script>
var interval = 500;
var timer = window.setInterval(function() {
var text = document.getElementById('myId').innerHTML;
var checkText = 't'
console.log(text);
console.log(checkText);
if(text == checkText){
alert("It has this text");
//do something
};
}, interval);
</script>
<div class="sar__label">
<div id="myId" class="text-split-original">t</div>
</div>
</html>
我故意删除了div中的所有空格。 您需要trim()空格,或操纵文本以匹配大小写。
一个很好的插件可以在https://j11y.io/javascript/monitoring-dom-properties/上找到 -是的,它是通过setInterval / clearInterval实现的。 但是一个简单的watch()插件的想法让我很高兴。
当然,它的版本较旧(大约2009年),但仍然有效。
/********** * jQuery plugin to allow monitoring of any * DOM el's properties. It does so by * a setInterval/clearInterval hook. Taken from * https://j11y.io/javascript/monitoring-dom-properties/ **********/ jQuery.fn.watch = function(id, fn) { return this.each(function() { var self = this; var oldVal = self[id]; $(self).data( 'watch_timer', setInterval(function() { if (self[id] !== oldVal) { fn.call(self, id, oldVal, self[id]); oldVal = self[id]; } }, 100) ); }); return self; }; jQuery.fn.unwatch = function(id) { return this.each(function() { clearInterval($(this).data('watch_timer')); }); }; /***** * This is the part that does stuff. * The updater updates that content div * and the contentEl.watch leverages the * above extensible plugin. It's watching * the vanilla 'innerText' attribute of * that DOM el. *****/ var contentEl = $(".content"); $(".updater").on("keyup", function() { contentEl.text($(this).val()) }) contentEl.watch("innerText", function(propName, oldVal, newVal){ console.log('Text has been changed from '+oldVal+' to ' + newVal); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content"> </div> <input type="text" class="updater" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.