国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

讓FireFox支持innerText的實現代碼

2024-05-06 14:12:38
字體:
來源:轉載
供稿:網友
為firefox實現innerText屬性
很多代碼寫了又忘忘了又寫,很浪費,所以決定養成做筆記的習慣。
知識點:
0、為什么要innerText?因為安全問題
1、為firefox dom模型擴展屬性
2、currentStyle屬性可以取得實際的style狀態
3、IE實現innerText時考慮了display方式,如果是block則加換行
4、為什么不用textContent?因為textContent沒有考慮元素的display方式,所以不完全與IE兼容
代碼如下:
<html>
<body>
<div id="d1"><a href="aa">ccc</a>ddd<div>eeee</div>fff</div>
<script type="text/javascript">
<!--
//
// patch of innerText for firefox
//
(function (bool) {
function setInnerText(o, s) {
while (o.childNodes.length != 0) {
o.removeChild(o.childNodes[0]);
}
o.appendChild(document.createTextNode(s));
}
function getInnerText(o) {
var sRet = "";
for (var i = 0; i < o.childNodes.length; i ++) {
if (o.childNodes[i].childNodes.length != 0) {
sRet += getInnerText(o.childNodes[i]);
}
if (o.childNodes[i].nodeValue) {
if (o.currentStyle.display == "block") {
sRet += o.childNodes[i].nodeValue + "/n";
} else {
sRet += o.childNodes[i].nodeValue;
}
}
}
return sRet;
}
if (bool) {
HTMLElement.prototype.__defineGetter__("currentStyle", function () {
return this.ownerDocument.defaultView.getComputedStyle(this, null);
});
HTMLElement.prototype.__defineGetter__("innerText", function () {
return getInnerText(this);
})
HTMLElement.prototype.__defineSetter__("innerText", function(s) {
setInnerText(this, s);
})
}
})(/Firefox/.test(window.navigator.userAgent));
//-->
</script>
<script type="text/javascript">
<!--
var d1 = document.getElementById("d1");
alert(d1.innerText);
d1.innerText = "xxx";
//-->
</script>
</body>
</html>


今天在制作firefox下支持復制的js代碼的時候,用到了innerText,測試發現原來firefox支持innerHTML但不支持innerText,所以上網找了一下,發現了一篇非常不錯的代碼。另從回復中,我們得到了如下兼容代碼。修正了原來ie下出現錯誤提示的問題。具體的看下么的文章。

把這段加在你所JS文件中就可以在MOZILLA/FIREFOX下使用innerText
代碼如下:
HTMLElement.prototype.__defineGetter__
(
"innerText",
function ()
{
var anyString = "";

var childS = this.childNodes;
for(var i=0; i<childS.length; i++)
{
if(childS[i].nodeType==1)
anyString += childS[i].tagName=="BR" ? '/n' : childS[i].innerText;
else if(childS[i].nodeType==3)
anyString += childS[i].nodeValue;
}
return anyString;
}
);


但這段代碼在IE中它會提示HTMLElement未定義,下面就是具體的解決方法。

代碼如下:
function isIE(){ //ie? 判斷是不是ie
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 新化县| 乐陵市| 镶黄旗| 察雅县| 台东市| 凤阳县| 凉城县| 彭水| 昌乐县| 东山县| 增城市| 珠海市| 澎湖县| 宜阳县| 滦南县| 石阡县| 温州市| 庆城县| 凌源市| 思南县| 潜江市| 原阳县| 安多县| 乃东县| 从江县| 中西区| 永和县| 齐齐哈尔市| 无为县| 孝义市| 镇雄县| 绥阳县| 桓台县| 兴城市| 临高县| 奉贤区| 陵川县| 察哈| 乌拉特前旗| 天峻县| 莫力|