不吃苹果 |
尽量采用W3C DOM 的写法
以前访问对象可能是:
document.all.apple 或者 apple
现在应该采用:
document.getElementById("apple") 以ID来访问对象,且一个ID在页面中必须是唯一的
document.getElementsByTagName("div")[0] 以标签名来访问对象
原来设置对象的属性可能是:
document.all.apple.width=100 或 apple.width=100
现在应该采用:
document.getElementById("apple").setAttribute("width","100")
document.getElementsByTagName("div")[0].setAttribute("width","100")
访问对象的属性则采用:
document.getElementById("apple").getAttribute("width")
document.getElementsByTagName("div")[0].getAttribute("width")
W3C DOM在IE下的一些限制
因为起先的IE占据整个浏览器95%的份额,没有竞争压力,所以这位老大就硬是要玩点另类,不完全按WEB标准来搞。
在IE下不能正确使用setAttribute来设置对象的style、class以及事件响应属性,
因此我还得按原来的点记法来访问和设置,以达到兼容各种浏览器的效果,如:
document.getElementById("banana").class
document.getElementById("banana").style.color
document.getElementById("banana").onclick
document.getElementById("banana").class="fruit"
document.getElementById("banana").style.color="blue"
document.getElementById("banana").onclick= function (){alert("我是香蕉")}
关于Firefox下的onload问题
function over(){
alert("页面加载完毕")
}
正常情况下,我们赋与onload响应函数是:
document.body.onload= over
但是在Firefox下这样无法执行,
所以我们都都采用下面这种形式:
window.onload=over
关于IE下TABLE无法插入新行的问题
IE下TABLE无论是用innerHTML还是appendChild插入
var row = document.createElement("tr");
var cell = document.createElement("td");
var cell_text = document.createTextNode("香蕉不吃苹果");
cell.appendChild(cell_text);
row.appendChild(cell);
document.getElementsByTagName("tbody")[0].appendChild(row);
当前1/3页123下一页阅读全文
相关推荐
- document.domain会导致ueditor拒绝访问解决办法
- Javascript实现代码折叠功能
- 深入浅出ES6之let和const命令
- PhotoSwipe异步动态加载图片方法
- 相册展示PhotoSwipe.js插件实现
- 移动端点击图片放大特效PhotoSwipe.js插件实现
- 手机端图片缩放旋转全屏查看PhotoSwipe.js插件实现
- 手机端 HTML5使用photoswipe.js仿微信朋友圈图片放大效果
- 手机图片预览插件photoswipe.js使用总结
- select隐藏选中值对应的id,显示其它id的简单实现方法
- Js得到radiobuttonlist选中值的两种方法(推荐)
- 总结JavaScript的正则与其他语言的不同之处
- js判断radiobuttonlist的选中值显示/隐藏其它模块的实现方法
- JS实现图片延迟加载并淡入淡出效果的简单方法
- JavaScript中闭包之浅析解读(必看篇)
- 微信JS接口大全
- JS解决iframe之间通信和自适应高度的问题
- 浅析Javascript ES6新增值比较函数Object.is
- js图片上传前预览功能(兼容所有浏览器)
- 前端程序员必须知道的高性能Javascript知识