Javascript设置img src [英] Javascript set img src

查看:215
本文介绍了Javascript设置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
}

然后我使用

  document["pic1"].src = searchPic;

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

然而,当我查询图像时,FireBug中的图像从未正确设置 [object HTMLImageElement] 作为图片的 src

However, the image is never set properly in FireBug when I query the image I get [object HTMLImageElement] as the src of the image

在IE I中获取:

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


推荐答案

您应该使用以下方法设置src:

You should be setting the src using this:

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

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

这篇关于Javascript设置img src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆