简体   繁体   English

如何在Javascript中获取div元素值?

[英]How to get div element values in Javascript?

 <html>
 <head>
 <script type="text/javascript">
 function caluculate(x)
  {
 var temp=x+1;
 var cn = document.getElementsByName(temp)[0].childNodes;
 alert( "attribute name :"+cn[0].name + "attributevalue :" + cn[0].nodeValue );  
  }
 </script>
 </head>
 <body>
 <div id="fn1">
  Enter your name: <input type="text" name="name" id="fn" onkeyup="caluculate(this.id)">
  Enter your age:  <input type="text" name="age" id="fa" onkeyup="caluculate(this.id)">
  NAME + AGE :<input type="text" name="nameage" id="fname" value="">
 </div>
 </body> </html>

How to get div element values in Javascript? 如何在Javascript中获取div元素值? I want to get the each values of input tag. 我想获取输入标签的每个值。

Please help me. 请帮我。

Try something like this. 尝试这样的事情。

HTML 的HTML

<div id="fn1">
    Enter your name: <input type="text" name="name" id="fn" onkeyup="caluculate()">
    Enter your age:  <input type="text" name="age" id="fa" onkeyup="caluculate()">
    NAME + AGE :<input type="text" name="nameage" id="fname" value="">
</div>

JavaScript 的JavaScript

function caluculate() {
    var cn = document.getElementById("fn1").getElementsByTagName("input");
    cn[2].value = "name: " + cn[0].value + ", age: " + cn[1].value;
}

Please try the following JavaScript function:- 请尝试以下JavaScript函数:-

function caluculate(x)
{
    var ele = document.getElementById(x);
    alert("attribute name: " + ele.name + ", attribute value: " + ele.value);
}

Since this JS function is always being called with the argument being the input element's ID, so there is no need to get / maintain the div element anywhere. 由于此JS函数始终以参数作为输入元素的ID来调用,因此无需在任何地方获取/维护div元素。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM