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