如何在 jQuery 中獲取資料屬性的值並使用它?
要獲取資料屬性的值,請使用 −
$(“yourSelector”).data()
以下是我們的 data 屬性輸入型別 −
<input type="text" id="txtValue" src="" data-value="90">
示例
以下是程式碼 −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <input type="text" id="txtValue" src="" data-value="90"> </body> <script> var value = $('#txtValue').data('value'); console.log("The value is=" + value); </script> </html>
輸出
輸出如下 −
廣告