我想创建一个js文件,并且必须动态加载img标签

时间:2019-04-02 10:30:55

标签: javascript html css

function loadApp(){

    var img = document.createElement("img");
    img.src = "images/bmw-918407_1280.jpg";
    src.appendChild(img);

}
window.onload = loadApp;

创建一个loadApp函数,该函数在文档加载时运行。在loadApp函数内部,我希望您调用一个函数:createImage(“ images / imageName.png”)。

如何执行此操作?

5 个答案:

答案 0 :(得分:1)

您还需要父项选择器。

// parentSelector is the parent of your image element. Pass the parameter whatever you want.
// imgSource is the URL of your image. Make sure it's appropriate image link (not broken)
function createAndPush(parentSelector, imgSource){
    var img = $('<img>');
    img.attr('src', imgSource);
    img.appendTo(parentSelector)
}

// Call in the onLoad or document ready.
createAndPush('#footer', 'https://cdn.sstatic.net/Img/unified/sprites.svg?v=e5e58ae7df45')

jQuery onReady中的调用函数如下:

$(document).ready(function(){
   createAndPush('#footer', 'https://cdn.sstatic.net/Img/unified/sprites.svg?v=e5e58ae7df45')
})

这是代码段

// parentSelector is the parent of your image element. Pass the parameter whatever you want.
// imgSource is the URL of your image. Make sure it's appropriate image link (not broken)
function createAndPush(parentSelector, imgSource){
    var img = $('<img>');
    img.attr('src', imgSource);
    img.appendTo(parentSelector)
}


$(document).ready(function(){
   createAndPush('#footer', 'https://cdn.sstatic.net/Img/unified/sprites.svg?v=e5e58ae7df45')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="footer">

</div>

答案 1 :(得分:1)

您可以使用以下preverse CLRF on Sql Server Management Studio query result

window.onload= e => document.body.innerHTML
    += '<img src="http://lorempixel.com/630/150/">';

答案 2 :(得分:0)

例如这样的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <script>
    function addImage(src){
        var newimg = document.createElement("img");
        newimg.setAttribute("src", src);
        document.getElementsByTagName("body")[0].appendChild(newimg);
    } 
    window.onload = addImage("images/bmw-918407_1280.jpg");
    </script>
</body>
</html>

答案 3 :(得分:0)

这是您下面的javascript文件。将其包含在HTML中的标记之前

function loadApp(){
  let img = document.createElement("img");
  img.src = "images/bmw-918407_1280.jpg";
  document.getElementsByTagName('body')[0].appendChild(img);
}

document.onreadystatechange = function () {
  if (document.readyState == "interactive") {
    loadApp();
  }
}

答案 4 :(得分:0)

这是异步下载。它应该每3秒下载一次图片。

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (4 == xmlhttp.readyState) {
if (xmlhttp.status >= 200 && xmlhttp.status < 300) {
      xmlhttp.open('GET', downloadUrl, true);
      xmlhttp.responseType = 'blob';
  } else {
      console.log("http error: " + xmlhttp.status);
  }
}
};
xmlhttp.timeout = 3000;