如何使用 jQuery 替換元素的內容?


要使用 jQuery 替換元素的內容,請使用 text() 方法。

示例

你可以嘗試執行以下程式碼以替換 div 中的內容

線上演示

<!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 p').text('The replaced text.');
 });
});
</script>
</head>
<body>

<div id="demo">
   <div class="test"></div>
   <p>The initial text</p>
</div>
<button id="button1">Replace Text</button>

</body>
</html>

更新於: 17-12-2019

527 檢視次數

啟動你的 職業生涯

完成課程並獲得認證

開始
廣告