English 中文(简体)
努力增加2个数字,但是在标数中加以总结。
原标题:Trying to add two numbers but they are wrapped in span tags
  • 时间:2011-10-15 09:02:03
  •  标签:
  • javascript

我正试图增加两个数字,但是,这些数字在我的网页上是动态的,在标签上被总结。 谁能告诉我我我我我,我可以补充这些数字吗?

这将是两个动态生成的数字的例子:

id子是 AL生的。

<span id="thmr_35" class="thmr_call"> 42</span>
<span id="thmr_42" class="thmr_call"> 11</span>

I need to add the two numbers 42 + 11 and then spit out: Total = 53

感谢你们的帮助!

问题回答

您可查阅<代码>span 内容,getElementById,并使用inner Rainbow/code> 财产获取这些内容。 之后,你可以使用<代码>parseInt,将这些指示数改为编号,并加在一起:

var num1 = document.getElementById("thmr_35").innerHTML,
    num2 = document.getElementById("thmr_42").innerHTML,
    added = parseInt(num1, 10) + parseInt(num2, 10);

http://jsfiddle.net/K3A9K/6/“rel=“nofollow”>。

如果补贴像你所说的那样是动态产生的,那么你可以通过名称获取这些要素。

var elems = document.getElementsByClassName( thmr_call );
var sum = 0;
for (var i = 0;i < elems.length; i++) {
  sum += parseInt(elems[i].innerHTML, 10);
}
alert(sum);

http://jsfiddle.net/K3A9K/7/

If also the class name is dynamically generated you can get the elements by the tag name. getElementsByTagName http://jsfiddle.net/K3A9K/8/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签