JQuery将2个div的值添加到第三个div中 [英] JQuery Adding the values of 2 divs into a third div

查看:181
本文介绍了JQuery将2个div的值添加到第三个div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个div,每个都有数字...

I have 3 divs with numbers in each...

<div id="one">1</div>

<div id="two">5</div>

<div id="total">0</div>

我需要做的是:

如果点击了#one,则添加#one和#two的值并在#total上更新

If #one is click then Add the values of #one and #two and update it on #total

因此,在上面的情况下,总计看起来像这样:

So, in the case above total would look like this:

<div id="total">6</div>


推荐答案

$('#one').click(function(){
$("#total").text(
    parseFloat($(this).text()) + 
    parseFloat($("#two").text())
);
});

这篇关于JQuery将2个div的值添加到第三个div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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