tags:

views:

56

answers:

5

Here is my code:

function init(){
    $.get(URL, function(data) {
     DATA = data;
     var srcWithHead = URL + "/" + $(DATA).find("img").attr("src");
     $(DATA).find("img").attr("src", srcWithHead);
     console.log("src = " + $(DATA).find("img").attr("src"));
    });
}

And I would like to have a picture showing. But the URL image src is a relative path. So, I need to have a header of each image. But it seems that $(DATA).find("img").attr("src", srcWithHead); doesn't work.

What can I do? and any other method? Thanks

A: 

I was working with attr today and the following works for me.

 $('#ctl00_Content_imgbtnDiscardQuote')
    .attr('src','').attr('value','Cancel').attr('type','submit');

Try running $(DATA); in firebug or chrome console, it may not be returning what you think.

Edit try .each()

$(DATA).find('img').each(function(index){ 
    $(this).attr('src','/myimg.jpg'); 
});

Welcome to stack overflow.

Keyo
Sorry, I can't get it.
Ian
because in my $(DATA), I have more than img item. So, I need to find it first.I am not sure what you are talking. Could you explain more? And you can solve it, right?Thx, I am a new one.
Ian
I don't see why it wouldn't work with multiple items. However you can try using $(DATA).each();See the edit above.
Keyo
I looked up the documentation. Seems you were correct, it only works on one element.".attr() ... Description: Get the value of an attribute for the first element in the set of matched elements."
Keyo
Thanks, i am trying each()but it seems that there is some error
Ian
A: 

Could it be that you're not finding any elements?

Try

alert($(DATA));
alert($(DATA).length);
alert($(DATA).find('img').length);
David Hedlund
No, I find it and it can show the pages. But the src doesn't change. It remains the same.
Ian
@Ian: what do you mean by *show the pages*? what pages? what i'm saying is that if either `$(DATA)` or `.find('img')` returns an empty object, then your code will work, but no images will be changed. please note that if you're trying to change more than one image, you're changing them all to *same* src with this code, as the `srcWithHead` variable will only hold one src. if you want to make a change relative to each picture, and you're on jquery1.4, you can do `$(DATA).find('img').attr('src', function() { return URL + '/' + $(this).attr('src'); });`.
David Hedlund
also note (altho this is probably not the cause of your problems) that you're greating a *global* variable `DATA` here, since you're introducing it without the `var` keyword. why not use the `data` variable you've already got, instead?
David Hedlund
A: 

try to eval your data, i'm not sure here since I don't know what would be the data be, but give this a try.

function init(){
    $.get(URL, function(data) {
       DATA = eval("("+data+")"); // treat the data as json object
       var srcWithHead = URL + "/" + $(DATA).find("img").attr("src");
       $(DATA).find("img").attr("src", srcWithHead);
       console.log("src = " + $(DATA).find("img").attr("src"));
    });
}
rob waminal
+1  A: 

You probably have multiple IMG tags. Use each() to access each one.

function init(){
    $.get(URL, function(data) {
    DATA = data;
    $(DATA).find("img").each(function() {
        var srcWithHead = URL + "/" + $(this).attr("src");
        $(DATA).find("img").attr("src", srcWithHead);
    });
}
Milan Babuškov
THXIt seems work. But only for the console. It cannot show too. At least the path is correct.
Ian
A: 

THX all of you~~

Ian