图像不附加到

时间:2017-12-17 15:33:32

标签: javascript html

我正在创建一个简单的图像列表。

HTML

<body onload="getImg()">
</body>

的JavaScript

function getImg() {
  var list = document.createElement("ul");
  list.className = "img-list";
  for (i = 0; i < 32; i++) {
      var img = document.createElement("img");
      var item = document.createElement("li");
      path = "images/DT/"+i+".png";
      img.src = path;
      img.alt = "something";
      item.appendChild(img);
      list.appendChild(item);
  }
console.log(list);
window.alert(list.innerHTML);
}

然后在控制台中,似乎我有列表显示但不知何故图像没有显示在网页中。蚂蚁想法发生了什么?

screenshot

2 个答案:

答案 0 :(得分:1)

document.body.appendChild(list);

仍然缺失。

function getImg() {
  var list = document.createElement("ul");
  list.className = "img-list";
  for (i = 0; i < 32; i++) {
    var img = document.createElement("img");
    var item = document.createElement("li");
    path = "images/DT/" + i + ".png";
    img.src = path;
    img.alt = "something";
    item.appendChild(img);
    list.appendChild(item);
  }
  document.body.appendChild(list);
}
<body onload="getImg()"></body>

<强>提示

代码必须位于<head>

<head>
  <script>...</script>
</head>
<body onload="getImg()">...</body>

答案 1 :(得分:0)

试试这个: JS:

public class CommonFunctions
{
    public void ValidateInput(Object sender){
        // do your validation logic
    }
}

public class CustomTextBox : TextBox 
{
    private CommonFunctions _commonFunctions;

    public CustomTextBox(CommonFunctions commonFunctions)
    {
            _commonFunctions = commonFunctions;
    }

    private void Control_TextChanged(Object sender,EventArgs e)
    {
              _commonFunctions.ValidateInput(sender);
    }

}

public class CustomNumericUpDown : NumericUpDown 
{
    private CommonFunctions _commonFunctions;

    public CustomNumericUpDown(CommonFunctions commonFunctions)
    {
            _commonFunctions = commonFunctions;
    }

    private void Control_TextChanged(Object sender,EventArgs e)
    {
              _commonFunctions.ValidateInput(sender);
    }
}

HTML:

<script>
    function getImg() {
        var list = document.createElement("ul");
        list.className = "img-list";
        for (i = 0; i < 2; i++) {
            var img = document.createElement("img");
            var item = document.createElement("li");
            path = "images/DT/" + i + ".png";
            img.src = path;
            img.alt = "something";
            item.appendChild(img);
            list.appendChild(item);
        }
        console.log(list);
        document.getElementById("page").innerHTML = list.innerHTML;
    }
</script>