jQuery 中有哪些方法可以操作屬性?


jQuery 提供了許多操作屬性的方法。這些都是與 DOM 相關的方法。屬性包括:

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

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

更新於: 2020-06-13

102 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.