如何使用 jQuery 获取 div 元素的内容值?
jqueryweb developmentfront end technology
要在 jQuery 中获取 div 元素的内容值,请使用 text() 方法。text() 方法会获取所有匹配元素的文本内容总和。此方法适用于 XML 和 XHTML 文档。
示例
您可以尝试运行以下代码,使用 jQuery 获取 div 元素的内容值:
<!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() {
var res = $('#demo').text();
alert(res);
});
});
</script>
</head>
<body>
<div id="demo"> This is <b>demo</b> text.
</div>
<button id="button1">Get</button>
</body>
</html>

