if (document.createEvent){//火狐写法
var evObj = document.createEvent('MouseEvents');
evObj.initEvent( 'click', true, false );
obj.dispatchEvent(evObj);
}else{//ie写法
obj.click();
}
}
ie比较简单,但不规范,火狐代码多一点,但是规规矩矩
但是似乎`
ie比较简单,但不规范,火狐代码多一点,但是规规矩矩
但是似乎`
方法一:div.removeNode(true);//火狐不支持
方法二:div.parentNode.removeChild(div);//IE火狐都支持
此方法也可以用来删除别的标签
按键兼容
在动态生成一个对象的onclick事件时我们都是这样写
element.onclick=functionName;
例:a.onclick=set(a);
这样写法参数是无法传递的
而且有自动被执行的问题(不自动执行可以这样写:a.onclick=set,只写方法名)
找到了一个小方法解决了问题
element.onclick=function() {
functionName(param);
};
例:
a.onclick=function(){
set(a);
}
但是又遇到一个问题,当参数时动态变化时,比如上例中a要是在循环中动态改变的,会出现每次循环a的值都是最后一次的值
后来查到另外一种写法据说叫做闭包?
a.onclick = (
function(param){
return function(){
set(param);
}
})(param)
解决了这个问题
附上keycode表
|
今天做一个页面,一直在firefox上面测试,等到终于完工了,想起到IE上跑一遍试试
一跑问题就出来了
找到原因问题出在getElementsByName这个方法上
IE无法使用getElementsByName只能用getElementsByTagName
于是只好加上了一个方法
function getElementsByName(tag,eltname){
var elts=document.getElementsByTagName(tag);
var count=0;
var elements=[];
for(var i=0;i<elts.length;i++){
if(elts[i].getAttribute("name")==eltname){
elements[count++]=elts[i];
}
}
return elements;
}
还好改动不是特别大 加上这个方法就解决问题了
(改掉原先使用document.getElementsByName的地方为getElementsByName)
还是有点怪怪的
0: (Uninitialized) the send( ) method has not yet been invoked.
1: (Loading) the send( ) method has been invoked, request in progress.
2: (Loaded) the send( ) method has completed, entire response received.
3: (Interactive) the response is being parsed.
4: (Completed) the response has been parsed, is ready for harvesting.
0 - (未初始化)还没有调用send()方法
1 - (载入)已调用send()方法,正在发送请求
2 - (载入完成)send()方法执行完成,已经接收到全部响应内容
3 - (交互)正在解析响应内容
4 - (完成)响应内容解析完成,可以在客户端调用了
readyState Status Code |
Status of the XMLHttpRequest Object |
---|---|
(0) UNINITIALIZED |
The object has been created but not initialized. (The open method has not been called.) |
(1) LOADING |
The object has been created, but the send method has not been called. |
(2) LOADED |
The send method has been called, but the status and headers are not yet available. |
(3) INTERACTIVE |
Some data has been received. Calling the responseBody and responseText properties at this state to obtain partial results will return an error, because status and response headers are not fully available. |
(4) COMPLETED |
All the data has been received, and the complete data is available in the responseBody and responseText properties. |
比如在点完保存以后显示正在保存的状态时应该判断readyState == 2的时候,执行显示的状态
今天写了一段JavaScript的代码让我很奇怪
<html>
<head>
<script language="javascript">
var userName = document.getElementById('username');
function foo(){
alert(userName.value);
return false;
}
</script>
</head>
<body>
<form action="a.php" method="POST" onsubmit="return foo();">
<input type="text" name='username' id='username' />
<input type="submit" value="提交">
</form>
</body>
</html>
点击提交时应该弹出userName的值,但这段代码弹的值却是null
查了半天谷歌终于找到了原因
"放在对应的id后面就可以了。虽然你的userName 是全局变量,但是如果你放在获取dom的前面的话,获得到的值就是null。"
恍然大悟,于是把代码修改了一下 将script代码放到了</body>下面,马上可以用了,ie,FireFox都没问题
<html>
<head>
</head>
<body>
<form action="a.php" method="POST" onsubmit="return foo();">
<input type="text" name='username' id='username' />
<input type="submit" value="提交">
</form>
</body>
<script language="javascript">
var userName = document.getElementById('username');
function foo(){
alert(userName.value);
return false;
}
</script>
</html>
那个人还有一个建议"但做法还是建议在执行操作时临时获得对象,如果放在body后面的话对资源是浪费的。"
这句话我还需要理解理解,呵呵,毕竟还是JavaScript的新手嘛
Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com