jquery img active的新id

时间:2013-09-16 12:06:43

标签: jquery

我正在使用此代码: http://jsfiddle.net/fGHmF/2/

现在我想在每个IMG ID中添加“size”这个词。例如:

    <img src='images/size1.png' alt='img1' id='size1' />
    <img src='images/size2.png' alt='img2' id='size2' />
    <img src='images/size3.png' alt='img3' id='size3' />
    <img src='images/size4.png' alt='img4' id='size4' />

我需要在java脚本中更改什么?

4 个答案:

答案 0 :(得分:6)

$('img').attr('id', function() {
   return 'size' + this.id;
});

答案 1 :(得分:2)

DEMO

$('img').attr('id', function() {
   return 'size'+this.id;
});

答案 2 :(得分:1)

尝试

$('img').each(function(){
    $(this).attr('id', 'size' + $(this).attr('id'))
})

答案 3 :(得分:0)

尝试以下

$('img').each(function(i){
   $(this).attr('id', 'size' + $(this).attr('id')); 
});

请参阅here