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 $this = $(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});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top