标签:
<!-- 实例010实现浏览器兼容改内容的函数 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>实例010实现浏览器兼容改内容的函数</title>
</head>
<body>
<!-- 内容(通常使用的四个属性) a div b h1 input用value
innerText只有IE好用,非IE用(textContent),不兼容
属性可以改值,也可以取值 -->
<div id="test" onclick="change()">你好!</div>
<script>
var obj = document.getElementById("test");
function change() {
// 可以兼容的改值
// intext(obj,"handsomehan");
// 可以兼容的获取值
// alert(intext(obj));
//obj.innerHTML = "<h1>我是handsomehan</h1>" //innerHTML这个是兼容的;
alert(obj.innerHTML);
alert(obj.outerHTML);
}
function intext(name,value) {
//如何区分IE和非IE,IE中的document中有all这个属性
if (document.all){
if (typeof(value) == "undefined") {
return name.innerText;
}else {
name.innerText = value;
}
} else {
if (typeof(value) == "undefined") {
return name.textContent;
} else {
name.textContent = value;
}
}
}
</script>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/handsomehan/p/5366145.html