简体   繁体   中英

Jquery attr src does not include img tag

var $card = $("<li>");
var imgName1 = 'images/' + rankOut + suitOut + '.png';
//assign element an image attribute
$card.attr("src", imgName1);

So I'm trying to add an image to my .card with attr.("src"...) however when the webpage loads, it is missing the <img> tag

It renders as <li src="images/ace_of_spades.png"></li> without the img tag. I asked my teacher and it has to look like this

<ul><img src="(image location)"></ul>

how do i add the <img> ?

试试这个:

var $card = $('<li><img src="' + 'images/' + rankOut + suitOut + '.png' + '"></li>');

You are currently setting the src attribute of an li element which of course doesn't create an img element. For creating an img element using jQuery you can code:

var $img = $('<img/>', {
   'src': 'images/' + rankOut + suitOut + '.png'
});
// Appending the element
$card.append($img);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM