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

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

innerHTML和innerText的用法

2024-07-21 02:04:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
用法:
<div id="test">
<span style="color:red">test1</span> test2
</div>


在js中可以使用:

test.innerhtml:

  也就是從對(duì)象的起始位置到終止位置的全部?jī)?nèi)容,包括html標(biāo)簽。
  上例中的test.innerhtml的值也就是“<span style="color:red">test1</span> test2 ”。

test.innertext:

  從起始位置到終止位置的內(nèi)容, 但它去除html標(biāo)簽
  上例中的text.innertest的值也就是“test1 test2”, 其中span標(biāo)簽去除了。

test.outerhtml:

  除了包含innerhtml的全部?jī)?nèi)容外, 還包含對(duì)象標(biāo)簽本身。
  上例中的text.outerhtml的值也就是<div id="test"><span style="color:red">test1</span> test2</div>

完整示例:
<div id="test">
<span style="color:red">test1</span> test2
</div>

<a href="javascript:alert(test.innerhtml)">innerhtml內(nèi)容</a>
<a href="javascript:alert(test.innertext)">inerhtml內(nèi)容</a>
<a href="javascript:alert(test.outerhtml)">outerhtml內(nèi)容</a>


特別說(shuō)明:

  innerhtml是符合w3c標(biāo)準(zhǔn)的屬性,而innertext只適用于ie瀏覽器,因此,盡可能地去使用innerhtml,而少用innertext,如果要輸出不含html標(biāo)簽的內(nèi)容,可以使用innerhtml取得包含html標(biāo)簽的內(nèi)容后,再用正則表達(dá)式去除html標(biāo)簽,下面是一個(gè)簡(jiǎn)單的符合w3c標(biāo)準(zhǔn)的示例:
<a href="javascript:alert(document.getelementbyid('test').innerhtml.replace(/<.+?>/gim,''))">無(wú)html,符合w3c標(biāo)準(zhǔn)</a>
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长沙市| 内江市| 梁河县| 布拖县| 密云县| 吉木萨尔县| 华蓥市| 金秀| 潞西市| 读书| 滁州市| 和龙市| 靖远县| 建水县| 宁海县| 大冶市| 惠东县| 凌海市| 贵南县| 鹿邑县| 克东县| 宜昌市| 基隆市| 平果县| 左权县| 南陵县| 西安市| 盘锦市| 北宁市| 寻甸| 南涧| 神木县| 鸡西市| 凉山| 石泉县| 颍上县| 清丰县| 柳江县| 外汇| 霍山县| 黄梅县|