[英]javascript: value increase continuously
I knew it's just a simple problem. 我知道这只是一个简单的问题。 But I still can't fix it.
但是我仍然无法解决它。 What I want is the output value will increase continuously when I click the button, and here's my code:
我想要的是,当我单击按钮时,输出值将不断增加,这是我的代码:
$('.submit').live('vclick', function(){
var x = 0;
x+=1;
document.getElementById('plus').innerHTML = x;
});
Thanks in advance 提前致谢
Try like this 这样尝试
var x = 0;
$('.submit').on('vclick', function(){
x = parseInt(document.getElementById('plus').innerHTML);
x += 1;
document.getElementById('plus').innerHTML = x;
});
Dont use live
because it is depricated .Instead use on
in place of live
. 不要用
live
,因为它是depricated。相反使用on
代替live
。 Try this FIDDLE 试试这个FIDDLE
Better use jQuery.data to store some variables point to DOM element 最好使用jQuery.data存储一些指向DOM元素的变量
$(document).on('vclick', '.submit', function(){
var el = $('#plus');
var x = $(el).data('x') || 0;
el.html(++x);
$(this).data('x', x);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.