如何使用 jQuery 僅替換 div 中的文字?


要僅使用 jQuery 替換 div 中的文字,請使用 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').html('Demo text');
   });
});
</script>
</head>
<body>

<div id="demo">This is it!</div>
<button id="button1">Replace</button>
</body>
</html>

更新於:17-12-2019

5 千次以上瀏覽

開啟 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.