jQuery attr() 方法


jQuery 中的 attr() 方法用於設定或返回所選元素的屬性和值。

語法

語法如下 −

$(selector).attr(attribute)

示例

現在讓我們看一個示例來實現 jQuery attr() 方法 −

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         alert("Input Type (Student Id) = " + $("input").attr("type"));
      });
   });
</script>
</head>
<body>
<h2>Student Details</h2>
Student Id: <input type="text"><br>
<button>Click me</button>
<p>Click on the button above to return the type of the input.</p>
</body>
</html>

輸出

這將產生以下輸出 −

單擊上述按鈕以顯示輸入文字的型別 −

更新於: 2019 年 11 月 12 日

232 次瀏覽

啟動你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.