常用的js在ie和firefox兼容写法
2008-06-16 10:12
513 查看
1.获取控件用document.getElementById,不用document.all(FF等浏览器不支持)
2.<button> 会被firefox解释为提交form或者刷新页面,需要写标准<button type="button">
3.使用childNode()代替之前的children
4. 手型鼠标指针请用cursor:pointer,不用 cursor:hand
5.获取自己定义的属性 用 document.getElementByID("TD1").getAttribute("isOBJ") 代替document.getElementByID("TD1").isOBJ
6.事件追加方法attachEvent(IE)/detachEvent;addEventListener( Mozilla, Netscape, Firefox)/removeEventListener
又或者直接用obj.onmouseover=func;
7.Firefox中不存在 Event时间,必须通过object本身去取
在Firefox获取当前物件的坐标方法:
document.onmousemove = Inti_move;
function Inti_move(ert)
{
x=ert.pageX;
}
表4 Mozilla与IE之间的事件属性差异
8>event.keyCode的問題解決
在FF中不存在window.event.keyCode屬性。可以用以下方法解決
此文转载于:http://www.cnblogs.com/talantlee/archive/2007/04/26/727951.html
----------------------------------------------------------------------------------------------------------
window.event
IE:有window.event对象
FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event)
鼠标当前坐标
IE:event.x和event.y。
FF:event.pageX和event.pageY。
通用:两者都有event.clientX和event.clientY属性。
鼠标当前坐标(加上滚动条滚过的距离)
IE:event.offsetX和event.offsetY。
FF:event.layerX和event.layerY。
标签的x和y的坐标位置:style.posLeft 和 style.posTop
IE:有。
FF:没有。
通用:object.offsetLeft 和 object.offsetTop。
窗体的高度和宽度
IE:document.body.offsetWidth和document.body.offsetHeight。注意:此时页面一定要有body标签。
FF:window.innerWidth和window.innerHegiht,以及document.documentElement.clientWidth和document.documentElement.clientHeight。
通用:document.body.clientWidth和document.body.clientHeight。
添加事件
IE:element.attachEvent(”onclick”, func);。
FF:element.addEventListener(”click”, func, true)。
通用:element.onclick=func。虽然都可以使用onclick事件,但是onclick和上面两种方法的效果是不一样的,
onclick只有执行一个过程,而attachEvent和addEventListener执行的是一个过程列表,也就是多个过程。例如:
element.attachEvent(”onclick”, func1);element.attachEvent(”onclick”,
func2)这样func1和func2都会被执行。
标签的自定义属性
IE:如果给标签div1定义了一个属性value,可以div1.value和div1[”value”]取得该值。
FF:不能用div1.value和div1[”value”]取。
通用:div1.getAttribute(”value”)。
父节点、子节点和删除节点
IE:parentElement、parement.children,element.romoveNode(true)。
FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。
2.<button> 会被firefox解释为提交form或者刷新页面,需要写标准<button type="button">
3.使用childNode()代替之前的children
4. 手型鼠标指针请用cursor:pointer,不用 cursor:hand
5.获取自己定义的属性 用 document.getElementByID("TD1").getAttribute("isOBJ") 代替document.getElementByID("TD1").isOBJ
6.事件追加方法attachEvent(IE)/detachEvent;addEventListener( Mozilla, Netscape, Firefox)/removeEventListener
又或者直接用obj.onmouseover=func;
7.Firefox中不存在 Event时间,必须通过object本身去取
在Firefox获取当前物件的坐标方法:
document.onmousemove = Inti_move;
function Inti_move(ert)
{
x=ert.pageX;
}
Internet Explorer Name | Mozilla Name | Description |
---|---|---|
altKey | altKey | Boolean property that returns whether the alt key was pressed during the event. |
cancelBubble | stopPropagation() | Used to stop the event from bubbling farther up the tree. |
clientX | clientX | The X coordinate of the event, in relation to the element viewport. |
clientY | clientY | The Y coordinate of the event, in relation to the element viewport. |
ctrlKey | ctrlKey | Boolean property that returns whether the Ctrl key was pressed during the event. |
fromElement | relatedTarget | For mouse events, this is the element from which the mouse moved away. |
keyCode | keyCode | For keyboard events, this is a number representing the key that was pressed. It is 0 for mouse events. |
returnValue | preventDefault() | Used to prevent the event's default action from occurring. |
screenX | screenX | The X coordinate of the event, in relation to the screen. |
screenY | screenY | The Y coordinate of the event, in relation to the screen. |
shiftKey | shiftKey | Boolean property that returns whether the Shift key was pressed during the event. |
srcElement | target | The element to which the event was originally dispatched. |
toElement | currentTarget | For mouse events, this is the element to which the mouse moved. |
type | type | Returns the name of the event. |
在FF中不存在window.event.keyCode屬性。可以用以下方法解決
function keepKeyNum(evt) !input type="text" onKeyPress=" return keepKeyNum(event);" >
此文转载于:http://www.cnblogs.com/talantlee/archive/2007/04/26/727951.html
----------------------------------------------------------------------------------------------------------
window.event
IE:有window.event对象
FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event)
鼠标当前坐标
IE:event.x和event.y。
FF:event.pageX和event.pageY。
通用:两者都有event.clientX和event.clientY属性。
鼠标当前坐标(加上滚动条滚过的距离)
IE:event.offsetX和event.offsetY。
FF:event.layerX和event.layerY。
标签的x和y的坐标位置:style.posLeft 和 style.posTop
IE:有。
FF:没有。
通用:object.offsetLeft 和 object.offsetTop。
窗体的高度和宽度
IE:document.body.offsetWidth和document.body.offsetHeight。注意:此时页面一定要有body标签。
FF:window.innerWidth和window.innerHegiht,以及document.documentElement.clientWidth和document.documentElement.clientHeight。
通用:document.body.clientWidth和document.body.clientHeight。
添加事件
IE:element.attachEvent(”onclick”, func);。
FF:element.addEventListener(”click”, func, true)。
通用:element.onclick=func。虽然都可以使用onclick事件,但是onclick和上面两种方法的效果是不一样的,
onclick只有执行一个过程,而attachEvent和addEventListener执行的是一个过程列表,也就是多个过程。例如:
element.attachEvent(”onclick”, func1);element.attachEvent(”onclick”,
func2)这样func1和func2都会被执行。
标签的自定义属性
IE:如果给标签div1定义了一个属性value,可以div1.value和div1[”value”]取得该值。
FF:不能用div1.value和div1[”value”]取。
通用:div1.getAttribute(”value”)。
父节点、子节点和删除节点
IE:parentElement、parement.children,element.romoveNode(true)。
FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。
相关文章推荐
- 项目中常用到的JS(兼容IE和Firefox)
- 几种pc端页面 常用的js兼容ie写法,几个名词 nodeName nodeValue及value addEventListener attachEvent
- 项目中常用到的JS(兼容IE和Firefox)
- 在IE 和 Firefox 中兼容使用js
- js实时时间显示,兼容IE、Opera、Firefox,javascript面向对象编程。
- JS关闭窗口兼容Firefox、Chrome、IE(二次总结后)
- 一个超简单的JS拖拽实现代码(兼容IE,Firefox)
- JS实时显示当前时间(IE+firefox)浏览器兼容代码(图证)
- 如何用JS获取键盘上任意按键的值?兼容FireFox和IE js获取键盘ASCII码?js键盘事件全面控制
- JS在IE和Firefox的不兼容问题解决方法总结
- JS中如何传递Event对象,兼容IE和FireFox
- 【Javascript】二、IE/FF的JS兼容写法
- js 键盘记录实现(兼容FireFox和IE)
- JS在IE和FireFox之间常用函数的区别小结
- js脚本:ie和firefox的兼容
- JS对select动态添加options操作[IE&FireFox兼容]
- js下IE和FF的一些兼容写法总结
- Js 键盘记录实现(兼容FireFox和IE)
- [IE&FireFox兼容]JS对select操作
- 兼容IE和FF的js脚本做法(比较常用)