jQuery attr使用来自object的img src

时间:2017-05-16 05:39:04

标签: javascript jquery for-loop attr

我的目标是遍历一个对象列表并在每个列循环方法中显示图像src和name但是不成功。我已经看过attr(“src”,“image src”)方法,但没有在循环中使用。我也可能在html中使用了错误的类...我是否正确地接近这个?我是jQuery的新手。谢谢你的帮助。

HTML:

<div class="container" style="max-width:800px;">
            <h2 align="center" id="character-text">Choose your character:</h2>
            <div class="row" id="charOptions" style="max-width:800px;" align="center">
                <div class="col-lg-3 char-img"></div>
                <div class="col-lg-3 char-img"></div>
                <div class="col-lg-3 char-img"></div>
                <div class="col-lg-3 char-img"></div>
            </div>

JS:

//Objects
var hansolo = {
    name: "Han Solo",
    attack: 10,
    hp: 20,
    counter: 0,
    src: "assets/images/hansolo.jpg",
}

var chewy = {
    name: "Chewy",
    attack: 10,
    hp: 20,
    counter: 0,
    src: "assets/images/chewy.jpg",
}

var jabba = {
    name: "Jabba",
    attack: 10,
    hp: 20,
    counter: 0,
    src: "assets/images/jabba.jpg",
}

var greedo = {
    name: "Greedo",
    attack: 10,
    hp: 20,
    counter: 0,
    src: "assets/images/greedo.jpg",
}

var choices = [hansolo, chewy, jabba, greedo];

for (var i = 0; i < choices.length; i++) {
    var charOptions = $("<charOptions>");
    charOptions
        .addClass( "char-img char-text")
        .attr("src", choices[i].src)
        .text(choices[i].name);        
    $(".charOptions").append(charOptions);
    console.log(charOptions);
}

3 个答案:

答案 0 :(得分:0)

我相信您想循环遍历对象列表并在每列中显示图像src和名称?

首先,删除HTML标记中的所有.col-lg- *,因为我们应该动态创建它。

然后,只需遍历您的对象并创建要追加的相关HTML对象。

var charOptionsRow = $('#charOptions');
$.each(choices, function(index, choice) {
  // Create a new div.col-lg-3 to be appended to row.
  var charOptionCol = $('<div>')
    .addClass('char-option col-lg-3');

  // Append image to col.
  var charImg = $('<img>')
    .addClass('char-img')
    .attr('src', choice.src);
  charOptionCol.append(charImg);

  // Append text to col.
  var charText = $('<h5>')
    .addClass('char-text')
    .text(choice.name);
  charOptionCol.append(charText);

  // Append column to row.
  charOptionsRow.append(charOptionCol);
});

答案 1 :(得分:0)

您似乎正在尝试创建img代码

同样img是一个自动关闭标签&amp; void element.you你可能无法添加text。我猜您正在寻找alt属性

for (var i = 0; i < choices.length; i++) { tag
    // creating img
    var charOptions = $("<img>");
    charOptions
        .addClass( "char-img char-text")
        .attr("src", choices[i].src)
        .alt(choices[i].name);     
    // use id selector since there is no dom with class charOptions   
    $("#charOptions").append(charOptions);
    console.log(charOptions);
}

答案 2 :(得分:0)

jquery

//Objects
var hansolo = {
name: "Han Solo",
attack: 10,
hp: 20,
counter: 0,
src: "assets/images/hansolo.jpg",
}

var chewy = {
name: "Chewy",
attack: 10,
hp: 20,
counter: 0,
src: "assets/images/chewy.jpg",
}

var jabba = {
name: "Jabba",
attack: 10,
hp: 20,
counter: 0,
src: "assets/images/jabba.jpg",
}

var greedo = {
name: "Greedo",
attack: 10,
hp: 20,
counter: 0,
src: "assets/images/greedo.jpg",
}

var choices = [hansolo, chewy, jabba, greedo];

$(choices).each(function(){
        console.log(this.src);
        var div = "<div  class='col-lg-3 char-img'><img 
src="+this.src+" /></div>"
        $('#charOptions').append(div);
});







html

<div class="container" style="max-width:800px;">
        <h2 align="center" id="character-text">Choose your character:
</h2>
        <div class="row" id="charOptions" style="max-width:800px;" align="center">
            <div class="col-lg-3 char-img"  style="width:800px; color:red;" >a</div>
            <div class="col-lg-3 char-img">b</div>
            <div class="col-lg-3 char-img">c</div>
            <div class="col-lg-3 char-img">d</div>
        </div>
  

<强> https://jsfiddle.net/sunilsoma/p111hyk3/