簡體   English   中英

我想創建一個js文件,並且必須動態加載img標簽

[英]I want to create a js file and have to load img tag dynamically

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”)。

這個怎么做 ?

您還需要父項選擇器。

// 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> 

您可以使用以下箭頭功能

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

例如這樣的:

<!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>

這是您的以下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();
  }
}

這是異步下載。 它應該每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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM