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 年 6 月 13 日

102 次瀏覽

啟動你的 職業

學完課程即可獲得認證

開始
廣告
© . All rights reserved.