onblur函数擦除之前写入的函数

时间:2014-06-28 19:45:01

标签: javascript css

任何人都可以帮我解决这个问题很长一段时间。 我想要实现的是在ie8中的onblur事件上使文本框颜色发生变化。 下面的代码工作正常但是我在重写它之前一直在检索onblur事件的值。这意味着我希望在onblur上调用这些警报,但它们似乎已经消失/被覆盖。试过各种替换,但都失败了。 我用了这段代码:

<html>
  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
     <title>Style changes on INPUT text boxes</title>
     <script type="text/javascript">

      // Object to activate focus and blur style changes for INPUT text boxes in 
      // Internet Explorer
      var InputFocus = {
        classNameFocus  : "focus",
        classNameBlur   : "",

        initialize : function() {
          // Test for Internet Explorer, and version that supports standard DOM calls
          if (window.ActiveXObject && document.selection && document.all && document.getElementById && document.getElementsByTagName) {
            //alert("It's internet explorer!");
            var inputs = document.getElementsByTagName("input");
            var i = 0;
            var end = inputs.length;

            for (i; i < end; i++) {
              inputs[i].onfocus = function() {
                InputFocus.doFocus(this);
              };
              var onblur_funcs = "" + inputs[i].onblur;
        //alert(onblur_funcs);
        onblur_funcs_val = onblur_funcs.substring(onblur_funcs.indexOf("{") + 1, onblur_funcs.indexOf("}"));
        inputs[i].onblur = function() {
            //this.onblur = function() {onblur_funcs_val};                  <------ tried this. didn't work :(
            InputFocus.doBlur(this);
            //this.onblur = function() {onblur_funcs_val};                  <------ tried this. alas didn't work :'(
                            //onblur_funcs_val    <-- this didn't work as well
        };

      }
    }
  },

  doBlur : function(el) {
    el.className = el.className.replace(this.classNameFocus, this.classNameBlur);
    //el.onblur = function() {onblur_funcs_val};                    <------ tried this. didn't work :(
    el = null;
    //el.onblur = function() {onblur_funcs_val};                    <------ tried this. this too didn't work :(
  },

  doFocus : function(el) {
    el.className += this.classNameFocus;
  }

};

window.onload = function() {
  InputFocus.initialize();
};

</script>


<style type="text/css" media="screen">
input{font-family:MS Sans Serif,Arial; font-size:9pt; color:red;}
/* For standards browsers */
input[type="text"]:focus {
  border: 1px solid #f00;
  background-color: #FFBFFF;
}

/* For Internet Explorer */
input.focus {
  border: 1px solid #f00;
  background-color: #FFBFFF;
}

</style>
</head>
<body>
<form action="" method="get">
  <input type="text" onblur="alert('Called onblur1');alert('called onblur2');" size="20"><br/>
  <input type="text" onblur="alert('Called onblur3');alert('called onblur4');" size="20"><br/>
  <input type="text" onblur="alert('Called onblur5');alert('called onblur6');" size="20">
</form>

</body>
</html>

任何帮助都将不胜感激。

0 个答案:

没有答案