Jquery ID属性包含Selector和Variable

时间:2012-04-03 17:11:25

标签: jquery selector contains

我想搜索包含值和变量的图像ID。

我需要以下脚本类似于$(“[id * ='MYVALUE'+ x]”),但这不起作用。

for (x=0; x< imageList.length; x++) 
    {

    var imageSRC = imageList[x];

    //need this to be MYVALUE + the X val
    //search for IDs that contain MYVALUE + X
    $("[id*='MYVALUE']").attr('src', 'images/'+imageSRC);

};

<!-- These images are replaced with images from the array imageList-->
<img id="dkj958-MYVALUE0" src="images/imageplaceholder" />
<img id="mypage-MYVALUE1" src="images/imageplaceholder" />
<img id="onanotherpage-MYVALUE2-suffix" src="images/imageplaceholder" />

3 个答案:

答案 0 :(得分:3)

看看这个:http://jsfiddle.net/w2UUg/2/

$('[id*="MYVALUE' + x + '"]').attr('src', 'images/'+imageSRC);

答案 1 :(得分:2)

$("[id*='MYVALUE" + x + "']")

应该做你想做的事。对不起,格式很糟糕,我正试图在手机上打字。

答案 2 :(得分:0)

$('img').filter(function () {
    return this.id.match(/myvalue\d+/i);
}).attr('src', 'images/' + imageSRC);