开发者

extending the jquery .each loop

I have a jQuery function that finds all the images nested in a container DIV with the ID of #popupForm, looks at the SRC attribute and compares it to a hidden input that has it's value created by a server side variable. Once rendered if there's a match it'll do a replace on the SRC of the image, hense:

var thisTheme = $("#hiddenTheme").val();
var defaultTheme = "/midway/firstTheme/"

$('#popupForm img').each(function () {

    var thisSRC = $(this).attr('src');
    if (thisSRC.indexOf(defaultTheme) !== -1) {

        var new_src = $(this).attr('src').replace(defaultTheme, thisTheme);
        $(this).attr('src', new_src);

    }

});

Now this works great but I wish to extend the loop part of this code (the .each part) so not only does it look at images in the container DIV but all inputs (yes some of the inputs have a SRC attribute - please don't ask why)

I was thinking of adding an array like such

var arrValues = ["#popupForm img", "#popupForm input"];

and then chaning the selector partof the each to co开发者_C百科ntain the array, something like this

$(arrValues).each(function () {

Does anyone know the best way for me to amend the code for this, what I've tried thus far hasn't worked!

Thanks


Just use commas in your selector (similar to CSS):

$('#popupForm img, #popupForm input').each(function () { ... });


This is a way to extend the wrapper:

$('#popupForm img').add('#popupForm input[src!=""]')...

The [src!=""] ensures only those inputs are added, which has an src attribute.

Hope this helps!


Have you tried changing this line to the following:

$('#popupForm img, #popupForm input').each(function () {...

Hope it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜