jquery and .attri doesn't work?
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 开发者_JS百科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
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);
});
}
Could it be that you're not finding any elements?
Try
alert($(DATA));
alert($(DATA).length);
alert($(DATA).find('img').length);
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"));
});
}
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.
精彩评论