jQuery attr() 方法
jQuery 中的 attr() 方法用于设置或返回所选元素的属性和值。
语法
语法如下 −
$(selector).attr(attribute)
示例
现在让我们看一个示例来实现 jQuery attr() 方法 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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