相容IE & Firefox 的指令碼改本屬性值的檢測方法 [ 不支援Chrome ]

iteye_20954發表於2011-12-28
<!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"> <head> <title></title> </head> <body> <input id="chkTest" type="checkbox" checked="checked"/> <input type="button" value="更改" onclick="ChangeCHK()" /> <script type="text/javascript"> var chkTest = document.getElementById("chkTest"); if (chkTest.watch) { chkTest.watch("checked", function (id, oldval, newval) { alert(newval); return newval; }); } else { chkTest.onpropertychange = function () { alert(this.checked); } } </script> <script type="text/javascript"> function ChangeCHK() { var check = chkTest.checked; chkTest.checked = check == true ? false : true; } </script> </body> </html>

————————————————————————————————————————————————————————————

下面的程式碼支援所有的瀏覽器, 可惜效率不高。

————————————————————————————————————————————————————————————

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script> var insObj = new Array(); var timer = null; function $(id) { return document.getElementById(id) }; function changeInspector(id) { insObj[id] = "" + $(id).value; timer = setInterval("inspector('" + id + "')", 100); } function inspector(sid) { if ($(sid).value != insObj[sid]) { alert("property changed"); insObj[sid] = $(sid).value; } } function doinspector() { changeInspector("mm"); changeInspector("mm2"); } </script> </head> <body onload="doinspector()"> <input type="text" id="mm" value=""></input> <input type="text" id="mm2" value=""></input> <input type="button" onclick="document.getElementById('mm').value='aa'" value="改變值"></input> 在doinspector 函式裡 新增 就可以了 changeInspector(物件的ID); //例如 changeInspector("mm"); </body></html>


相關文章