JavaScript 设置 img src

发布于 2024-07-30 03:27:35 字数 654 浏览 4 评论 0原文

我可能错过了一些简单的东西,但当你读到的所有内容都不起作用时,这很烦人。 我的图像可能在动态生成的页面过程中重复多次。 因此,显而易见的一件事就是预加载它并始终使用该变量作为源。

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

设置图像

  document["pic1"].src = searchPic;

然后我使用或

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

但是,当我查询图像时,图像在 FireBug 中永远不会正确设置,我得到 [object HTMLImageElement] 作为图像

在 IE 中的 src我得到:

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

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

In IE I get:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

小情绪 2024-08-06 03:27:35

您应该使用以下命令设置 src:

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

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

You should be setting the src using this:

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

or

$("#pic1").attr("src", searchPic.src);
想你的星星会说话 2024-08-06 03:27:35

纯JavaScript手动创建img标签并添加属性

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);

pic1<中设置src /code>

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

或使用 getElementById

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

j-Query 来存档此内容,

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

Pure JavaScript to Create img tag and add attributes manually ,

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 pic1

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

or with getElementById

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

j-Query to archive this,

$("#pic1").attr("src", searchPic.src);
鲜肉鲜肉永远不皱 2024-08-06 03:27:35

图像构造函数的实例不适合在任何地方使用。 您只需设置src,图像就会预加载......就这样,演出结束了。 您可以丢弃该物体并继续前进。

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

是你应该做的。 您仍然可以使用图像构造函数,并在 searchPiconload 处理程序中执行第二个操作。 这可确保在实际 img 对象上设置 src 之前加载图像。

就像这样:

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
}

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. 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. This ensures the image is loaded before you set the src on the real img object.

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
}
£噩梦荏苒 2024-08-06 03:27:35
document["pic1"].src = searchPic.src

应该管用

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

should work

枯叶蝶 2024-08-06 03:27:35

另外,解决此问题的一种方法是使用 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);

备注:有一点是,Javascript 社区现在鼓励开发人员使用文档选择器,例如 querySelectorgetElementByIdgetElementsByClassName > 而不是文档[“pic1”]。

Also, one way to solve this is to use document.createElement and create your html img and set its attributes like this.

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"].

九局 2024-08-06 03:27:35

哇! 当您使用 src 时,还必须使用 searchPicsrc

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

看起来更好

Wow! when you use src then src of searchPic must be used also.

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

looks better

莫多说 2024-08-06 03:27:35

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

You don't need to construct a whole new image... the src attribute just takes a string value :-)

不即不离 2024-08-06 03:27:35

你需要设置

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

searchPic本身就是你的Image(),你需要读取你设置的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.

挽容 2024-08-06 03:27:35

您的 src 属性是一个对象,因为您将 src 元素设置为您在 JavaScript 中创建的整个图像。

尝试

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

Your src property is an object because you are setting the src element to be the entire image you created in JavaScript.

Try

document["pic1"].src = searchPic.src;
青朷 2024-08-06 03:27:35

如果您使用 WinJS 您可以更改 src通过Utilities功能。

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

If you're using WinJS you can change the src through the Utilities functions.

WinJS.Utilities.id("pic1").setAttribute("src", searchPic.src);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文