如何使用 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

25K+ 檢視次數

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告