如何从JS获取跨度标记中的值 [英] How To get values in Span Tag from JS

查看:99
本文介绍了如何从JS获取跨度标记中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTML制作BMI计算器。输入来自文本字段。我正在用Javascript函数进行一些计算。我想要像下面这样获得计算值:

 < span id =class =>您的计算结果是=< / span> 
< span id =class => 我想要结果在这里< / span>

===================== ===============================================
编辑:我询问的部分

 < script language =javascript> 
var bmi = document.getElementById(ID_bmiResult2)。innerHTML;
bmi.value = weight /(heightInMeter * heightInMeter);
< / script>

< body>
< span id =ID_bmiResult2> < /跨度>
< / body>

我不知道如何制作它...

解决方案

编辑:正如它指出的,FF不喜欢 innerText ,所以我将替换为 innerHTML ,在这种情况下,这并不会改变脚本的整体功能。



您可以获得第二个 span 并将值直接传递给 innerHTML ,如下所示:

  document.getElementsByTagName('span')[1] .innerHTML ='结果'; 

注意:考虑使用和 id class ,因此您可以找到需要更改的确切元素,而不是检索该类型的多个元素(在本例中为 span )。
如果你有一个 id ,它会是这样的:

  document.getElementById('id在这里')。innerHTML ='结果'; 

编辑2 :此编辑是在操作系统更改其问题
现在我看到你的代码变得非常混乱。
我写过 小提琴 ,以便您可以看到它工作

I am trying to make a BMI Calculator using HTML. The inputs come from text fields. I am making some calculation with them in a Javascript function. I want to get the calculated values in a span like this:

<span id="" class="">Your Calculation Results is = </span>
<span id="" class="">   "" I want the result here ""    </span>

==================================================================== Edit : The part that i am asking about

<script language="javascript">
var bmi = document.getElementById("ID_bmiResult2").innerHTML;
bmi.value = weight / (heightInMeter * heightInMeter);
</script>

<body>
<span id="ID_bmiResult2">  </span>
</body>

i don't know how to make it ...

解决方案

Edit: As it was pointed out, FF doesn't like innerText, so I'm replacing to innerHTML which in this particular case doesn't not change the overall functionality of the script.

You can get the 2nd span and pass the value directly to innerHTML like this:

document.getElementsByTagName('span')[1].innerHTML= 'the result';

Note: Consider using and id or class so you can find the exact element you need to change instead of retrieving several elements of that type (in this case span). If you had an id, it would be like this:

document.getElementById('the id goes here').innerHTML= 'the result';

Edit2: This edit was made after the OP changed his question It got really confusing now that I'm seeing your code. I've written this fiddle so you can see it working

这篇关于如何从JS获取跨度标记中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆