I have a script that takes the alt from a span, uses it as an src for an image. Takes the class of that span and transfers it to the img, and finally, takes the text of the span and uses it as an alt or title. For some reason the alt/title part isn't working. Even if i specify a static title/alt that each image would then have, it still doesn't show up.
I have 3 pieces of information that i need to preserve across these translations, class, name, and url. Alt/title would be a useful attribute with which to store the name portion. (since img tags don't have name space) I could technically use id, but these images are being cloned throughout the session.
var $t开发者_StackOverflow中文版his = $(this);
var src = $this.attr('alt');
var Name = $this.val();
var Type = $(this).attr('class');
$(target).parent().after($('<div class="Packet"></div>').append('<img src="'+ src +'" class="Border '+ Type +'" title="some title" />'));
The val()
method is only applicable to input fields to obtain the "value" attribute. If you want to get the text inside the span then use text()
or html()
. The former option ignores html tags.
Also, consider passing attributes to the second parameter when creating elements via jQuery:
var img = $('<img>', {'src': src, 'class': 'Border ' + Type, 'title': name});
精彩评论