如何使用 jQuery 獲取 div 內容的值?


要在 jQuery 中獲取 div 內容的值,請使用 text() 方法text() 方法 獲取所有匹配元素的組合文字內容。此方法適用於 XMLXHTML 文件

示例

您可以嘗試執行以下程式碼以使用 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>

更新日期:2023 年 10 月 22 日

2.5 萬+ 瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.