简体   繁体   English

在JavaScript中,显示数组中的图片吗?

[英]In JavaScript, Displaying pictures from an array?

I am making an rpg game. 我正在制作一个rpg游戏。 So I am trying to make a for loop that will generate buttons to the screen this what I have so far, 因此,我正在尝试创建一个for循环,该循环将为屏幕生成此按钮,到目前为止,

$(document).ready(function() {
    var imgArray = new Array();
    imgArray[0] = new Image();
    imgArray[0].src = 'assets/images/young_link.jpg';

    //var test = "<img src= '../images/young_link.jpg'>";
    //var young_hero = ["young_link","young_zelda","impa", "malon"];
    var young_hero = ["young_link"];
    //var hero_images = [test];

    for (var i = 0; i < young_hero.length; i++) {
        var hero_btns = $("<buttons>");
        hero_btns.addClass("hero");
        hero_btns.attr("data-hero" , young_hero[i]);
        hero_btns.attr("data-image" , imgArray[i]);
        hero_btns.text(imgArray[i]);
        hero_btns.text(young_hero[i]);
        $("#buttons").append(hero_btns);
    }
});

The problem is that it is not putting the image on the button. 问题在于它没有将图像放在按钮上。

In order to add an image to your button, you will have to do more than just data-image , You will need to actually create an <img> tag and give it the source of the image. 为了将图像添加到您的按钮,您不仅需要做data-image ,还需要实际创建一个<img>标签,并为其提供图像源。 Then you will need to use css to get the image lined up properly in your button. 然后,您将需要使用css在按钮中正确排列图像。

I have updated your code to add the image to the first button. 我已经更新了您的代码,以将图像添加到第一个按钮。

 $(document).ready(function() { var imgArray = new Array(); imgArray[0] = new Image(); imgArray[0].src = 'https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcT7o2tCpqvs_XRpvtHbuRe9KwkzydiVhWLJ6YVTkQcpkev09w0MBCgNu2w'; //var test = "<img src= '../images/young_link.jpg'>"; var young_hero = ["young_link","young_zelda","impa", "malon"]; //var young_hero = ["young_link"]; //var hero_images = [test]; for (var i = 0; i < young_hero.length; i++) { var hero_btns = $("<button>"); // changed from <buttons> hero_btns.addClass("hero"); hero_btns.attr("data-hero" , young_hero[i]); //hero_btns.text(imgArray[i]); hero_btns.text(young_hero[i]); hero_btns.append(imgArray[i]) $("#buttons").append(hero_btns); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="buttons"> </div> 

Not sure about your exact requirement. 不确定您的确切要求。 Seems you are trying to create a button inside another button. 似乎您正在尝试在另一个按钮中创建一个按钮。

If you want to add an image use img tag and its src attribute 如果要添加图像,请使用img标签及其src属性

Not tested but you can try this 未经测试,但您可以尝试

$(document).ready(function() {
  var imgArray = new Array();
  imgArray[0] = new Image();
  imgArray[0].src = 'assets/images/young_link.jpg';
  var young_hero = ["young_link"];
  for (var i = 0; i < young_hero.length; i++) {
    var hero_btns = $("<img>");
        hero_btns.addClass("hero");
        hero_btns.attr("data-hero", young_hero[i]);
    hero_btns.attr("src", imgArray[i]);
        hero_btns.text(imgArray[i]);
    hero_btns.text(young_hero[i]);

    $("#buttons").append(hero_btns);
  }
});
    $(function(){
var heroList = {
{heroName: "young_link", imagePath: "assets/images/young_link.jpg"},
{heroName: "young_zelda", imagePath: "assets/images/young_zelda.jpg"},
{heroName: "impa", imagePath: "assets/images/impa.jpg"},
{heroName: "young_link1", imagePath: "assets/images/young_link1.jpg"}];

 for (var i = 0; i < heroList.length; i++) {
        var hero_btns = $('<img class="hero">');

        hero_btns.attr({
        "data-index": i,
        "src": heroList[i].imagePath,
         "data-name": heroList[i].heroName,
         "title": heroList[i].heroName
        });
        $("#buttons").append(hero_btns);
    }

})

You can use this simple on jsfiddle example. 您可以在jsfiddle示例中使用此简单示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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