获取内容
- text() - 设置或返回所选元素的文本内容
- html() - 设置或返回所选元素的内容(包括 HTML 标记)
- val() - 设置或返回表单字段的值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<title>load方法</title>
</head>
<body>
<button id="b1">html</button>
<button id="b2">text</button>
<button id="b3">表单val</button>
<p id="c1">我是text</p>
<p id="c2">我是html</p>
<input id="c3">
<script type="text/javascript">
$("#b1").click(function () {
alert("Text: " + $("#c1").text());
});
$("#b2").click(function () {
alert("HTML: " + $("#c2").html());
});
$("#b3").click(function () {
alert("HTML: " + $("#c3").val());
});
</script>
</body>
</html>
获取属性
- attr() 方法用于获取属性值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<button>获取属性值</button>
<p><a href="https://www.baidu.com" id="c1">百度</a></p>
<script>
$(document).ready(function () {
$("button").click(function () {
alert($("#c1").attr("href"));
});
});
</script>
</body>
</html>