我試圖增加一個屬性onclick在jquery,每次1。增加屬性onclick在jquery
比方說,我有這個按鈕,它的頁面上唯一.new-rcc
:
<a class="new-rcc" data-attr="1" href="#"><span>Button »</span></a>
每次單擊按鈕時,我需要data-attr
1增加,所以它只是從1增加2〜3〜4等
我試圖做這樣的事情:
var showthese = $('.new-rcc').prop('data-attr');
$('.new-rcc').click(function(){
var showtotal = showthese++;
$(this).prop('data-attr', showtotal);
return false;
});
但它似乎沒有要工作?
我建議使用'.data('attr')'而不是'.prop('data-attr')'。這有很多原因,但主要是因爲如果將「data-attr」作爲屬性附加,jQuery將無法識別它,只有當它通過.data()附加時,纔會等待您的jQuery版本。 –