Javascript jQuery-检测布尔值的变化

Javascript jQuery-检测布尔值的变化,javascript,jquery,boolean,detect,Javascript,Jquery,Boolean,Detect,有没有办法通过jQuery检测全局布尔值是否从false变为true?是的,使用getter/setter对,setter会捕获变量的设置: 解决办法可能是 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtm

有没有办法通过jQuery检测全局布尔值是否从false变为true?

是的,使用getter/setter对,setter会捕获变量的设置:

解决办法可能是

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript Object - watch () method example</title>
</head>
<body>
<h1 style="color: red">JavaScript Object : watch() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ o = {x:10}
o.watch("x",
function (id,oldvalue,newvalue) {
document.writeln("o." + id + " changed from "
+ oldvalue + " to " + newvalue+"<br />")
return newvalue
})
o.x = 20
o.x = 30
o.x = 40
//]]>
</script>
</body>
</html> 

JavaScript对象-watch()方法示例
JavaScript对象:watch()方法

//这样做是为了使下面的JavaScript代码与XHTML兼容。
兼容:

  • Internet Explorer 7
  • 火狐3.6
  • 谷歌Chrome7
  • Safari 5.0.1
  • 歌剧10

请参见。

将函数回调数组保留为“观察者”。当通过接口函数(例如setBooleanValue(True);)更改布尔值时,请迭代回调数组并调用每个函数以通知观察者。

池是最简单的:虽然最简单并不总是最好的:)此站点习惯上不只是发布链接,但也要在你的答案中包含代码和细节,这样它就独立了。如果链接中断,您的答案对社区仍然有价值,并将独立存在。使用
编辑
链接继续改进您的答案:需要指出的一点是,watch只在Mozilla上运行,根据文档,它对性能有影响。他们建议进行调试。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript Object - watch () method example</title>
</head>
<body>
<h1 style="color: red">JavaScript Object : watch() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ o = {x:10}
o.watch("x",
function (id,oldvalue,newvalue) {
document.writeln("o." + id + " changed from "
+ oldvalue + " to " + newvalue+"<br />")
return newvalue
})
o.x = 20
o.x = 30
o.x = 40
//]]>
</script>
</body>
</html>