简体   繁体   English

Javascript 设置 img src

[英]Javascript set img src

I am probably missing something simple but it's quite annoying when everything you read doesn't work.我可能遗漏了一些简单的东西,但是当您阅读的所有内容都不起作用时,这很烦人。 I have images which may be duplicated many times over the course of a dynamically generated page.我的图像可能会在动态生成的页面过程中多次复制。 So the obvious thing to do is to preload it and use that one variable as the source all the time.所以显而易见的事情是预加载它并始终使用该变量作为源。

var searchPic;
function LoadImages() {
    searchPic = new Image(100,100);
    searchPic.src = "XXXX/YYYY/search.png";
    // This is correct and the path is correct
}

then I set the image using然后我使用设置图像

  document["pic1"].src = searchPic;

or或者

  $("#pic1").attr("src", searchPic);

However, the image is never set properly in FireBug when I query the image I get [object HTMLImageElement] as the src of the image但是,当我查询图像时,图像从未在 FireBug 中正确设置我得到[object HTMLImageElement]作为图像的src

In IE I get:在 IE 中,我得到:

http://localhost:8080/work/Sandbox/jpmetrix/[object]

You should be setting the src using this:您应该使用以下方法设置 src:

document["pic1"].src = searchPic.src;

or或者

$("#pic1").attr("src", searchPic.src);

Pure JavaScript to Create img tag and add attributes manually ,纯 JavaScript 创建img标签并手动add attributes

var image = document.createElement("img");
var imageParent = document.getElementById("body");
image.id = "id";
image.className = "class";
image.src = searchPic.src;            // image.src = "IMAGE URL/PATH"
imageParent.appendChild(image);

Set src in pic1pic1设置 src

document["#pic1"].src = searchPic.src;

or with getElementById或使用 getElementById

document.getElementById("pic1").src= searchPic.src;

j-Query to archive this, j-Query 来归档这个,

$("#pic1").attr("src", searchPic.src);

Instances of the image constructor are not meant to be used anywhere.图像构造函数的实例不打算在任何地方使用。 You simply set the src , and the image preloads...and that's it, show's over.您只需设置src ,然后图像预加载......就这样,显示结束。 You can discard the object and move on.您可以丢弃该对象并继续前进。

document["pic1"].src = "XXXX/YYYY/search.png"; 

is what you should be doing.是你应该做的。 You can still use the image constructor, and perform the second action in the onload handler of your searchPic .您仍然可以使用图像构造函数,并在searchPiconload处理程序中执行第二个操作。 This ensures the image is loaded before you set the src on the real img object.这可确保在您在真实img对象上设置src之前加载图像。

Like so:像这样:

function LoadImages() {
    searchPic = new Image();
    searchPic.onload=function () {
        document["pic1"].src = "XXXX/YYYY/search.png";
    }
    searchPic.src = "XXXX/YYYY/search.png"; // This is correct and the path is correct
}

Also, one way to solve this is to use document.createElement and create your html img and set its attributes like this.此外,解决此问题的一种方法是使用document.createElement并创建您的 html img并像这样设置其属性。

var image = document.createElement("img");
var imageParent = document.getElementById("Id of HTML element to append the img");
image.id = "Id";
image.className = "class";
image.src = searchPic.src;
imageParent.appendChild(image);

REMARK : One point is that Javascript community right now encourages developers to use document selectors such as querySelector , getElementById and getElementsByClassName rather than document["pic1"].备注:有一点是 Javascript 社区现在鼓励开发人员使用文档选择器,例如querySelectorgetElementByIdgetElementsByClassName而不是 document["pic1"]。

document["pic1"].src = searchPic.src

应该管用

你不需要构建一个全新的图像...... src 属性只需要一个字符串值 :-)

You need to set你需要设置

document["pic1"].src = searchPic.src;

The searchPic itself is your Image(), you need to read the src that you set. searchPic 本身就是你的 Image(),你需要读取你设置的 src。

Your src property is an object because you are setting the src element to be the entire image you created in JavaScript.您的 src 属性是一个对象,因为您将 src 元素设置为您在 JavaScript 中创建的整个图像。

Try尝试

document["pic1"].src = searchPic.src;

Wow!哇! when you use src then src of searchPic must be used also.当您使用src时, searchPic必须使用searchPic src

document["pic1"].src = searchPic.src

looks better看起来更好

如果您使用的是WinJS,您可以通过Utilities函数更改src

WinJS.Utilities.id("pic1").setAttribute("src", searchPic.src);

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

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