在 Jquery 中,如何提醒具有 id 的常规元素的值?

In Jquery, how do I alert the value of a regular element with an id?

   <!doctype html>
 <html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
<script type="text/javascript">
    $(document).ready(function(){var mainVal = $("#h1").valueOf();alert(mainVal);})</script>
</head>

<body>
<h1 id="h1" value="0">hallow you </h1>
 </body>
 </html>

我在这里尝试使用 val() 方法。任何帮助都是极好的。谢谢。

首先,您使用了 .valueOf()(它是一种不同的 js 方法,这是它的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf ) it should be .val() which is used to get the values of form elements such as input, select and textarea, for more info check the docs here: https://api.jquery.com/val/

其次,要从元素中的 value 属性获取值,您需要使用 .attr('attribute-name') 方法

  $(document).ready(function () {
    var mainVal = $("#h1").attr("value");
    alert(mainVal);
  })