jQuery text()
此方法獲取匹配元素集合中所有元素的文字內容,或設定匹配元素集合中每個元素的文字內容。
特別說明:
HTML內容就是內容中可以包含HTML標籤,並且能夠被瀏覽器渲染。
文字內容是先將內容中的HTML預定義字元轉換成html字元實體,這樣HTML標籤就不會被渲染。
語法結構一:
[JavaScript] 純文字檢視 複製程式碼.text()
獲取所有匹配元素的文字內容。
jQuery1.0版本新增。
語法結構二:
[JavaScript] 純文字檢視 複製程式碼.text(text)
引數解析:
text:用於設定匹配元素內容的文字。當提供的是一個數值或布林值得時候,將被轉換成一個字串表現形式。
設定所有匹配元素的文字內容。
jQuery1.0版本新增。
語法結構三:
[JavaScript] 純文字檢視 複製程式碼.text(function(index, oldText))
引數解析:
function:用來返回設定文字內容的一個函式。index:當前元素在元素集合中的索引(從0開始),oldText:當前元素原來的文字內容。函式中的this指向當前元素。
jQuery1.4版本新增。
程式碼例項:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { alert($("li").text()); }) }) </script> </head> <body> <ul> <li>螞蟻部落一</li> <li>螞蟻部落二</li> <li>螞蟻部落三</li> </ul> <input type="button" id="bt" value="檢視演示" /> </body> </html>
返回所有匹配元素文字內容的組合。
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { $("div").text("<strong>螞蟻部落</strong>"); }) }) </script> </head> <body> <div></div> <input type="button" id="bt" value="檢視演示" /> </body> </html>
設定div元素的文字內容,可以看到html標籤不會被瀏覽器渲染。
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { $("div").text(function () { return "<strong>螞蟻部落</strong>"; }); }) }) </script> </head> <body> <div></div> <input type="button" id="bt" value="檢視演示" /> </body> </html>
功能與上一個程式碼相同,只不過text文字內容是通過函式返回的。
相關文章
- jQuery之text()的實現jQuery
- jQuery html(),text(),val()區別jQueryHTML
- jQuery的html()和text()區別jQueryHTML
- jQuery獲得指定text的值jQuery
- JQuery 獲取select被選中的value和textjQuery
- text
- text/html和text/plain的區別HTMLAI
- Text Representation
- CSS 文字裝飾 text-decoration & text-emphasisCSS
- ORACLE TEXT(轉)Oracle
- DAN Text Classification
- text1
- text-to-motion
- jQuery初探:自制jQueryjQuery
- 我的’jQuery’和jQueryjQuery
- WPF Text MultiBinding StringFormatORM
- CSS text-strokeCSS
- CSS text-shadowCSS
- CSS text-overflowCSS
- Oracle Text簡介Oracle
- ASBFAT:Sublime Text 3
- Leetcode 68 Text JustificationLeetCode
- 深入char、varchar、text
- jQueryjQuery
- sublime text for Mac註冊啟用 sublime text4註冊碼Mac
- jQuery入門(三)--- jQuery語法jQuery
- jquery列印頁面(jquery.jqprint)jQuery
- JQuery模板外掛-jquery.tmpljQuery
- leetcode-68-Text JustificationLeetCode
- JavaScript select text 屬性JavaScript
- read content in a text file in pythonPython
- [LeetCode] 68. Text JustificationLeetCode
- SVG <text>繪製文字SVG
- CSS text-fill-colorCSS
- The Text Splitting CodeForces 612A
- Tkinter (19) 文字部件 Text
- Flutter之Text和ImageFlutter
- flutter隨筆- Text and StyleFlutter
- TADA! Text to Animatable Digital AvatarsGit