2015-07-05 16 views
1

我正在尋找以下問題的正確語法:如何按類選擇多個html元素並更改其不同的值?

我在頁面上獲得了一些與class =「power」相同的元素,我希望選擇它們並將它們的值提高3.我如何實現這在jQuery中?

我的解決方案,到目前爲止,設置所有的元素,以4

<div class="power">1</div> 
<div class="power">2</div> 
<div class="power">3</div> 
<div class="power">4</div> 



<script> 
    $('.power').html(foo($('.power').html())); 
    function foo(bla){ 
     var output = parseInt(bla)+3; 
     return output; 
    } 


</script> 

輸出

4 
4 
4 
4 

我想

4 
5 
6 
7 

我猜

回答

4

您可以使用.each()

$('.power').each(function(){ 
    var output = parseInt($(this).text())+3; 
    $(this).html(output); 
}); 
2

使用什麼樣的價值jQuery.each

$('.power').each(function() { 
    $(this).html(foo($(this).html())); 
}); 
相關問題