如何使用 jQuery 獲取 div 內容的值?
要在 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>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP