奇怪现象是:JS创建的<input>输入框的值第一次被改变时,没有触发onpropertychange事件。
请在IE浏览器下测试。<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
一般的输入框:<input type="text" value="1" id="input1" /><br />
JS创建的输入框:
<script language="javascript">var input1 = document.getElementById("input1");
input1.attachEvent("onpropertychange",f);var input2 = document.createElement("input");
input2.type = "text";
input2.value = "1";
document.body.appendChild(input2);
input2.attachEvent("onpropertychange",f);function f(){alert("!");}
</script>
</body>
</html>
请在IE浏览器下测试。<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
一般的输入框:<input type="text" value="1" id="input1" /><br />
JS创建的输入框:
<script language="javascript">var input1 = document.getElementById("input1");
input1.attachEvent("onpropertychange",f);var input2 = document.createElement("input");
input2.type = "text";
input2.value = "1";
document.body.appendChild(input2);
input2.attachEvent("onpropertychange",f);function f(){alert("!");}
</script>
</body>
</html>
解决方案 »
- 在一个静态页面上 显示 “注册和登录” ,登录后 显示 用户的信息 探讨求助
- 注册会员判断EMAIL或手机!!!急急急....
- 怎么在屏幕中间载入个文字动画。并等文字动画播放完毕执行后面程序再删除此动画
- 问个问题,这个JS函数有什么错误?谢谢
- //////求一个文章发布的可视化在线编辑器,主要是要有代码发布的功能(注:FCKEditor之类的不行),就相csdn的blog文章发布一样
- 谁能帮我这段vbscript改为javascript?
- 用js脚本生成的select框,不能触发onchange事件吗?
- 这样简单的javascript错在哪_(急~~)
- Help Me
- 请教两个问题?
- 通过new Date来拿到月份中的天数的问题
- 学JavaScript要有什么基础?
初步推测是一个BUG. 但解决方法还是有的.
var input2 = document.createElement('<input type="text" value = "1">');
input2.attachEvent("onpropertychange",f);
document.body.appendChild(input2);
<script language="javascript">
var input1 = document.getElementById("input1");
input1.attachEvent("onpropertychange",f);
var input2 = document.createElement("<input type='text' value='1'/>");
//input2.type = "text";
//input2.value = "1";
document.body.appendChild(input2);
input2.attachEvent("onpropertychange",f);
function f(){alert("!");}
</script>
当代码成这样时,同样的问题又来了。var input1 = document.getElementById("input1");
input1.attachEvent("onpropertychange",f);
var input2 = document.createElement('<input type="text" value = "1">');
//input2.type = "text";
//input2.value = "1";
input2.attachEvent("onpropertychange",f);
var input3 = input2.cloneNode(true);//复制节点以及事件
document.body.appendChild(input3);
function f(){alert("!");}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
一般的输入框:<input type="text" value="1" id="input1" /><br />
JS创建的输入框:
<script language="javascript">var input1 = document.getElementById("input1");
input1.attachEvent("onpropertychange",f);
var input2 = document.createElement('<input type="text" value = "1" onpropertychange="f()">');document.body.appendChild(input2);
function f(){alert("!");}</script>
</body>
</html>
var input2 = document.createElement('<input type="text" value = "1" onpropertychange="f()">');其他浏览器就addEventListener,事件都不一样,所以这个判断过程是必须的<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
一般的输入框:<input type="text" value="1" id="input1" oninput="alert(this.value)"/><br />
JS创建的输入框:
<script language="javascript">
var input2 ;
if(document.all)input2= document.createElement('<input type="text" value = "1" onpropertychange="f()">');//IE
else {//其他浏览器
input2=document.createElement('input');
input2.type="text";
input2.value="1";
if(navigator.userAgent.indexOf('Firefox')!=-1)input2.addEventListener('input',f,false);//FF的是oninput,其他的浏览器不知道是否支持,所以你还得判断下
}document.body.appendChild(input2);
function f(){alert("!");}</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
一般的输入框:<input type="text" value="1" id="input1" /><br />
JS创建的输入框:
<script language="javascript">
var input1 = document.getElementById("input1");
input1.attachEvent("onpropertychange",f);
var input2 = document.createElement('<input type="text" value = "1" onpropertychange="f();">');
var input3 = input2.cloneNode(true);//复制节点以及事件
document.body.appendChild(input3);
function f(){alert("!");}
</script>
</body>
</html>
貌似没想到啥好办法.而且你所说的cloneNode能够复制事件 是不对的. 那是IE attchEvent的bug. 你恰恰把BUG当做需求用了. 实在不行IE就只能放弃clone了
这样创建:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
一般的输入框:<input type="text" value="1" id="input1" /><br />
JS创建的输入框:
<script language="javascript">function f(){alert("!22");}var input1 = document.getElementById("input1");
input1.attachEvent("onpropertychange",f);
var input2 = document.createElement("<input type='text' value='1'>");
var input3 =document.body.appendChild(input2);
input3.attachEvent("onpropertychange",f);</script>
</body>
</html>