jQuery 中的 html() 方法的作用是什麼?


html() 方法設定或返回選定元素的內容。jQuery 有些方法用於操作屬性,包括 html() 方法。這些是與 DOM 相關的函式。這些屬性包括:

  • text() – 此方法設定或返回選定元素的文字內容。
  • html() – 此方法設定或返回選定元素的內容。
  • val() – 此方法設定或返回表單域的值。

示例

您可以嘗試執行以下程式碼以瞭解如何使用 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>

更新日期:14-2-2020

115 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.