如何使用 jQuery 設定元素的 HTML 內容?


要設定元素的 HTML 內容,請使用 html() 方法。你可以嘗試執行以下程式碼,以便使用 jQuery 獲取元素的 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(){
        $("#demo").html("<strong>This text is highlighted.</strong>");
    });
});
</script>
</head>
<body>

<p id="demo">This is a paragraph.</p>
<button id="button1">Click to set HTML</button>

</body>
</html>

更新於:17-Feb-2020

678 Views

啟動你的 事業

透過完成課程獲得認證

開始
廣告