--- id: 564944c91be2204b269d51e3 title: 使用 jQuery 更改元素內部的文本 challengeType: 6 forumTopicId: 16773 dashedName: change-text-inside-an-element-using-jquery --- # --description-- 可以通過 jQuery 改變元素開始和結束標籤之間的文本。 甚至改變 HTML 標籤。 jQuery 有一個 `.html()` 函數,能用其在標籤裏添加 HTML 標籤和文本, 函數提供的內容將完全替換之前標籤的內容。 下面是重寫並強調標題文本的代碼: ```js $("h3").html("jQuery Playground"); ``` jQuery 還有一個類似的函數 `.text()`,可以在不添加標籤的前提下改變標籤內的文本。 換句話說,這個函數不會評估傳遞給它的任何 HTML 標記,而是將其視爲要替換現有內容的文本。 給 id 爲 `target4` 的按鈕的文本添加強調效果。 查看這篇[關於 <em> 的文章](https://www.freecodecamp.org/news/html-elements-explained-what-are-html-tags/#em-element)來了解更多 `` 和 `` 的區別和用法。 注意,`` 標籤雖然傳統上用來強調文本,但此後常用作圖標的標籤。 `` 標籤作爲強調標籤現在已被廣泛接受。 可以使用任意一種完成這個挑戰。 # --hints-- 應該通過添加 HTML 標籤強調 `target4` 按鈕中的文本。 ```js assert.isTrue( /|\s*#target4\s*<\/em>|<\/i>/gi.test($('#target4').html()) ); ``` 文本應該保持不變。 ```js assert($('#target4') && $('#target4').text().trim() === '#target4'); ``` 不應該改變其它任何文本內容。 ```js assert.isFalse(/|/gi.test($('h3').html())); ``` 應該使用 `.html()` 方法而不是 `.text()` 方法。 ```js assert(code.match(/\.html\(/g)); ``` 應該使用 jQuery 選取 `button id="target4"`。 ```js assert(code.match(/\$\(\s*?(\"|\')#target4(\"|\')\s*?\)\.html\(/)); ``` # --seed-- ## --seed-contents-- ```html jQuery Playground #left-well #target1 #target2 #target3 #right-well #target4 #target5 #target6 ``` # --solutions-- ```html jQuery Playground #left-well #target1 #target2 #target3 #right-well #target4 #target5 #target6 ```