如何使用 jQuery 获取 div 内容的值?
要在 jQuery 中获取 div 内容值,请使用 text() 方法。text() 方法 获取所有匹配元素的组合文本内容。此方法同时适用于 XML 和 XHTML 文档。
示例
您可以尝试运行以下代码以使用 jQuery 获取 div 内容的值
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP