Javascript 如何从 JS 获取 Span 标签中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11266990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To get values in Span Tag from JS
提问by Mahmoud
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:
我正在尝试使用 HTML 制作一个 BMI 计算器。输入来自文本字段。我正在 Javascript 函数中用它们进行一些计算。我想在这样的跨度中获取计算值:
<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 ...
我不知道怎么做...
回答by MilkyWayJoe
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.
编辑:正如有人指出的那样,FF 不喜欢innerText
,所以我要替换innerHTML
它,在这种特殊情况下不会改变脚本的整体功能。
You can get the 2nd span
and pass the value directly to innerHTML
like this:
您可以获得第二个span
并将值直接传递给innerHTML
这样的:
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:
注意:考虑使用 andid
或者class
这样你可以找到你需要更改的确切元素,而不是检索该类型的几个元素(在这种情况下span
)。如果你有一个id
,它会是这样的:
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 fiddleso you can see it working
Edit2:这个编辑是在 OP 改变他的问题之后进行的 现在我看到你的代码真的很困惑。我写了这个小提琴,所以你可以看到它的工作
回答by Hymanwanders
If you have a variable like calcResult
, you can do:
如果你有一个像 的变量calcResult
,你可以这样做:
document.getElementById('spanId').innerHTML += calcResult;
Of course, that only works once. If you do it again, you'll have two results in the span. If you want to be able to change the result if the form fields change, I'd try:
当然,这只有效一次。如果你再做一次,你会在跨度中得到两个结果。如果您希望能够在表单字段更改时更改结果,我会尝试:
<span id="calc">Your calculation result = <span></span></span>
var result = 12;
document.getElementById('calc').getElementsByTagName('span')[0].innerHTML = result;
回答by Lucas_Santos
回答by Nicholas Decker
You can use document.writeln to get your result. check out the fiddle on http://jsfiddle.net/CmVww/to play with it. Below are the results.
您可以使用 document.writeln 来获得结果。查看http://jsfiddle.net/CmVww/上的小提琴来玩它。以下是结果。
<div id="start">
<span id="one" class="sText">Your Calculation Results is = </span>
<span id="two" class="result">
<script type="text/javascript">
var tst = 'I want the result here';
document.writeln(tst);
</script>
</span>
回答by Sashank Mishra
var bmi = weight / (heightInMeter * heightInMeter);
document.getElementById('ID_bmiResult2').innerHTML= bmi;
回答by Ajay Yadav
For calculate the bmi in javascript
用于计算javascript中的bmi
<script language="javascript">
var Height = 0;
var Weight = 0;
var BMI = 0;
function getTotalBMIScore() {
Height = document.getElementById('MainContent_txtPDHeight').value;
//alert(Height + 'Height');
Weight = document.getElementById('MainContent_txtPDWeight').value;
//alert(Weight + 'Weight')
BMI = Weight / (Height * Height);
BMI = BMI.toFixed(2);
//alert(BMI + 'BMI');
document.getElementById('BMIScore').innerText = BMI;
}
</script>
// end function
// 结束函数
// if you are taking from textbox you should use this Height(In meter):
// 如果您是从文本框中获取,则应使用此高度(以米为单位):
<asp:TextBox ID="txtPDHeight" onchange ="getTotalBMIScore()" onKeyUp ="getTotalBMIScore()" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" runat="server"></asp:TextBox>
<span>
Weight(In kg):
重量(公斤):
<asp:TextBox ID="txtPDWeight" onchange = "getTotalBMIScore()" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" onKeyUp ="getTotalBMIScore()" runat="server"></asp:TextBox>
//if you want to display in span use this BMI:
//如果要在跨度中显示,请使用此BMI:
<span id="BMIScore"></span></span><br />