兼容IE & Firefox 的脚本改本属性值的检测方法 [ 不支持Chrome ]

来源:互联网 发布:贤者之爱结局知乎 编辑:程序博客网 时间:2024/05/16 19:39
<!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>



原创粉丝点击