innerHTML、outerHTML、innerText、outerText的区别
2006-12-05 15:22
375 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>无标题文档</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script language="javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var innertext="欢迎来到我的博客 你调用的div对象还没有消失";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var outertext="欢迎来到我的博客 你调用的div对象已经没有消失不信你试一试";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function change_outerText(objectID)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var divTextObject=document.getElementById(objectID);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(!divTextObject)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("outerText(): The "+objectID+" not found.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
divTextObject.outerText=outertext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function change_innerText(objectID)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var divTextObject=document.getElementById(objectID);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(!divTextObject)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("innerText(): The "+objectID+" not found.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
divTextObject.innerText=innertext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div id="div"><button>Hello techno-dragon</button><font color="green"><h2>This is a DIV!</h2></font></div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<br>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input name="innerHTML" value="innerHTML" type="button" OnClick="alert(div.innerHTML);">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input name="outerHTML" value="outerHTML" type="button" OnClick="alert(div.outerHTML);">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input name="innerText" value="innerText" type="button" OnClick="alert(div.innerText);">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input name="outerText" value="outerText" type="button" OnClick="alert(div.outerText);">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<br>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
由于上边无法解释innerText 和 outerText的区别所以看下面的按钮
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<br>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input name="change_outerText" value="outerText" type="button"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
OnClick="change_outerText('div');">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input name="change_innerText" value="innerText" type="button"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
OnClick="change_innerText('div');">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上边代码运行就知道区别了^_^;
总结
inner操作的是tag内部的对象, outer 不仅操作内部对象也包含了本身对象.读这些属性值的过程中,可能没有区别,但是赋值操作区别可就大了.
相关文章推荐
- innerHTML、outerHTML、innerText、outerText的区别
- JS中innerHTML、outerHTML、innerText、outerText的用法与区别
- innerHTML、outerHTML、innerText、outerText的区别
- InnerHTML、InnerText、outerHTML、outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别
- innerHTML、innerText和outerHTML、outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别
- innerHTML、innerText和outerHTML、outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别
- innerHTML、innerText和outerHTML、outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别及兼容性问题
- innerHTML、innerText和outerHTML、outerText的区别
- innerHTML、innerText、outerHTML、outerText的区别
- innerHTML、innerText和outerHTML、outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别
- javascript初学者笔记——关于innerHTML、innerText、outerHTML和outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别
- innerHTML、outerHTML、innerText、outerText的区别
- javascript innerHTML、outerHTML、innerText、outerText的区别