简体   繁体   English

不断返回[object HTMLSpanElement]? (JavaScript)

[英]Keeps returning [object HTMLSpanElement]? (Javascript)

Ok so im trying to add two (or more) variables and make it appear in a table cell. 好的,我尝试添加两个(或更多)变量并使它出现在表单元格中。 I get the variables from lets say the 1st and 2nd cells and i want to display them on the 7th cell, so this is what i have for now. 我从第1个和第2个单元格得到变量,我想在第7个单元格上显示它们,所以这就是我现在所拥有的。 This is the basic code for the cell that gets the info. 这是获取信息的单元的基本代码。

function prompta1()
    {
      var a1=prompt("Unesi broj","")
      var spana1 = document.getElementById ("spana1");
      spana1.innerHTML = a1;
function prompta2()
    {
      var a2=prompt("Unesi broj","")
      var spana2 = document.getElementById ("spana2");
      spana2.innerHTML = a2;

And this is the code for the cell that needs to show them. 这是需要显示它们的单元格的代码。

  function functiona7()
    {
      var a7= spana1+spana2;
      var spana7 = document.getElementById ("spana7");
      spana7.innerHTML = a7;
    } 

Now , when i try this it only gives. 现在,当我尝试这个时,它只会给出。

[object HTMLSpanElement][object HTMLSpanElement] [object HTMLSpanElement] [object HTMLSpanElement]

Please help! 请帮忙! HTML for getting the values 用于获取值的HTML

enter code here   <tr>
  <td><h1>1</h1></td>

  <td><input type="button" onclick="prompta1()" value="Izmeni"> <br> <span id='spana1'></span></td>
  <td><input type="button" onclick="prompta2()" value="Izmeni"> <br> <span id='spana2'></span></td>
</tr>
<tr>

HTML for showing the value 用于显示价值的HTML

      <td>   <input type="button" onclick="functiona7()" value="Izracunaj"> <br> <span id='spana7'></span> </td>

Try something like this: 尝试这样的事情:

function prompta1() {
    var a1 = prompt("Unesi broj", "");
    var spana1 = document.getElementById("spana1");
    spana1.innerHTML = a1;
}

function prompta2() {
    var a2 = prompt("Unesi broj", "");
    var spana2 = document.getElementById("spana2");
    spana2.innerHTML = a2;
}

function functiona7() {
    var spana1 = document.getElementById("spana1").innerHTML;
    var spana2 = document.getElementById("spana2").innerHTML;
    var spana7 = document.getElementById("spana7");
    if (!(spana1 && spana2 && spana7)) return;
    var a7 = spana1 + spana2;
    spana7.innerHTML = a7;
}

Fiddle 小提琴

In your current example the values for spana1/2 are not pulling from the DOM, so first they need to be populated using getElementById, after that a simple combination of the Inner Html is all that is needed (although you may want to add a space in between too). 在您当前的示例中,spana1 / 2的值不是从DOM中提取的,因此首先需要使用getElementById填充它们,此后,仅需要内部HTML的简单组合即可(尽管您可能想添加一个空格)在两者之间)。

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

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