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>輸出
這將產生以下輸出 −

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

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP