JavaScript 设置 img src
我可能错过了一些简单的东西,但当你读到的所有内容都不起作用时,这很烦人。 我的图像可能在动态生成的页面过程中重复多次。 因此,显而易见的一件事就是预加载它并始终使用该变量作为源。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您应该使用以下命令设置 src:
或
You should be setting the src using this:
or
纯JavaScript手动创建
img
标签并添加属性
,在
pic1<中设置src /code>
或使用 getElementById
j-Query 来存档此内容,
Pure JavaScript to Create
img
tag andadd attributes
manually ,Set src in
pic1
or with getElementById
j-Query to archive this,
图像构造函数的实例不适合在任何地方使用。 您只需设置
src
,图像就会预加载......就这样,演出结束了。 您可以丢弃该物体并继续前进。是你应该做的。 您仍然可以使用图像构造函数,并在
searchPic
的onload
处理程序中执行第二个操作。 这可确保在实际img
对象上设置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. You can discard the object and move on.is what you should be doing. You can still use the image constructor, and perform the second action in the
onload
handler of yoursearchPic
. This ensures the image is loaded before you set thesrc
on the realimg
object.Like so:
应该管用
should work
另外,解决此问题的一种方法是使用
document.createElement
并创建 html img 并设置其属性,如下所示。备注:有一点是,Javascript 社区现在鼓励开发人员使用文档选择器,例如
querySelector
、getElementById
和getElementsByClassName
> 而不是文档[“pic1”]。Also, one way to solve this is to use
document.createElement
and create your html img and set its attributes like this.REMARK: One point is that Javascript community right now encourages developers to use document selectors such as
querySelector
,getElementById
andgetElementsByClassName
rather than document["pic1"].哇! 当您使用
src
时,还必须使用searchPic
的src
。看起来更好
Wow! when you use
src
thensrc
ofsearchPic
must be used also.looks better
您不需要构建一个全新的图像... src 属性只需要一个字符串值:-)
You don't need to construct a whole new image... the src attribute just takes a string value :-)
你需要设置
searchPic本身就是你的Image(),你需要读取你设置的src。
You need to set
The searchPic itself is your Image(), you need to read the src that you set.
您的 src 属性是一个对象,因为您将 src 元素设置为您在 JavaScript 中创建的整个图像。
尝试
Your src property is an object because you are setting the src element to be the entire image you created in JavaScript.
Try
如果您使用 WinJS 您可以更改
src
通过Utilities
功能。If you're using WinJS you can change the
src
through theUtilities
functions.