$().thml()與$().text()的區別,.thml.text區別

來源:互聯網
上載者:User

$().thml()與$().text()的區別,.thml.text區別

<1>

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script src="http://localhost:41928/Jquery/jquery-1.10.2.js" type="text/javascript"></script>    <script type="text/javascript">                $(function () {            //.thml()是在塊中加html代碼            $("div").html("<a href='http://www.baidu.com'>百度</a>");             alert($("div").text());  //輸出是是一個百度的超連結        })        $(function () {            //.text()是在塊中的文字            $("div").text("<a href='http://www.baidu.com'>百度</a>");            alert($("div").text()); //輸出的是 【<a href='http://www.baidu.com'>百度</a>】這麼一段文字        })          </script></head><body><div></div></body></html>



http://blog.sina.com.cn/s/blog_561be372010008ev.html


js中innerHTML與innerText的用法與不同

用法:

<div id="test">
<span style="color:red">test1</span> test2
</div>

在JS中可以使用:

test.innerHTML:

  也就是從對象的起始位置到終止位置的全部內容,包括Html標籤。

  上例中的test.innerHTML的值也就是“<span style="color:red">test1</span> test2 ”。

test.innerText:

  從起始位置到終止位置的內容, 但它去除Html標籤

  上例中的text.innerTest的值也就是“test1 test2”, 其中span標籤去除了。

test.outerHTML:

  除了包含innerHTML的全部內容外, 還包含對象標籤本身。

  上例中的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內容</a>
<a href="javascript:alert(test.innerText)">inerHTML內容</a>
<a href="javascript:alert(test.outerHTML)">outerHTML內容</a>

特別說明:

  innerHTML是符合W3C標準的屬性,而innerText只適用於IE瀏覽器,因此,儘可能地去使用innerHTML,而少用innerText,如果要輸出不含HTML標籤的內容,可以使用innerHTML取得包含HTML標籤的內容後,再用Regex去除HTML標籤,下面是一個簡單的符合W3C標準的樣本:

<a href="javascript:alert(document.getElementById('test').innerHTML.replace(/<.+?>/gim,''))">無HTML,符合W3C標準</a>...餘下全文>>
 
jquery的html()\text()\val()之間的不同問題

html()是擷取包括html代碼的所有值,例如:<span>測試文字123</span>
text()是擷取不包括html代碼裡的值,例如:測試文字123
val()是擷取表單的值,例如input裡的:測試文字123
自己寫例子嘗試下就知道了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.