javascript - jquery检测文本变化

标签 javascript jquery html

嗨,我正在尝试检测 div 文本的值是否为“9”,然后提醒用户。但我无法让它工作。有没有办法检测新数字“9”?谢谢!

这是一个活生生的例子: http://jsfiddle.net/7gyL2h2e/5/

HTML:

<div id="number">8</div>
<div id="clickme">Click me +1</div>

Jquery:

$("#clickme").click(function(){
      $('#number').html(function(i, val) { return +val+1;});
});

if($.trim($("#number").text())=="9"){
  alert("number 9");  

}

最佳答案

只需将 if 条件移动到点击事件中即可。

在这里查看http://jsfiddle.net/7gyL2h2e/6/

HTML:

<div id="number">8</div>
<div id="clickme">Click me +1</div>

Jquery:

$("#clickme").click(function(){
  $('#number').html(function(i, val) { return +val+1;});
  if($.trim($("#number").text())=="9"){
    alert("number 9");  
  }
});

关于javascript - jquery检测文本变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386247/

相关文章:

javascript - 如何将行拆分为 x 个字符或更少,并且仅在空格 ("") 处?

javascript - localStorage 对象数组 : Only push new object into array if unique

internet-explorer - 您将如何在 IE 中为这种倾斜的 DIV 导航设置 CSS3 回退?

html - 100% 高度不适用于两个 div 之一

javascript - 在最后一个选定元素之后执行函数的 jQuery 插件

javascript - 使用 express 时在 EJS View 中查找当前 url

javascript - JQUERY Tablesorter 没有正确排序数字列

javascript - Bluebird promisifyAll 在普通情况下不起作用

javascript - 使用 jQuery 选择嵌套元素

jquery - 使用 jQuery 前置到标题标签?