jQuery 中的 text() 和 html() 有什麼區別?
jQuery 提供了許多用於處理屬性的方法。這些都是與 DOM 相關的函式。屬性包括 text() 和 html()。
- text() – 此方法設定或返回所選元素的文字內容。
- html() – 此方法設定或返回所選元素的內容。
示例
你可以嘗試執行以下程式碼來了解如何使用 text() 和 html() 方法處理屬性 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button1").click(function(){
alert("Using text()- " + $("#demo").text());
});
$("#button2").click(function(){
alert("Using html()- " + $("#demo").html());
});
});
</script>
</head>
<body>
<p id="demo">This is <b>demo</b> text.</p>
<button id="button1">Text</button>
<button id="button2">HTML</button>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式語言
C++
C#
MongoDB
MySQL
Javascript
PHP