[英]What is the proper way to use JQuery’s .load() on an image?
I am trying to use .load() to retrieve an image from the server after someone clicks a button on my page, then surround it in "a href" and "img" tags to turn the image into a clickable link, and finally append it all to a div. 有人单击我页面上的按钮后,我尝试使用.load()从服务器检索图像,然后将其包围在“ a href”和“ img”标签中,以将图像变成可点击的链接,最后附加全部归于div。
Below is the code I am using and it WORKS….. 以下是我正在使用的代码,它的工作原理是.....
$("<a href=\"" + ObjectName[randomNumber].link + "\" target=\"_blank\">
<img src=\"/contentimages/"+ ObjectName[randomNumber].imageurl +"\"></a>").load().appendTo("#imageDiv");
I then bind this function to a button so when the user clicks the button it executes. 然后,我将此功能绑定到按钮,以便用户单击该按钮时执行。
Both the image (ObjectName[randomNumber].imageurl) and link (ComboArray[randomNumber].link) are properties of an object which are chosen client-side via javascript. 图像(ObjectName [randomNumber] .imageurl)和链接(ComboArray [randomNumber] .link)都是对象的属性,可以通过javascript在客户端选择。 Imageurl is a straight up .png file ("image1.png") in a folder. Imageurl是文件夹中的纯正.png文件(“ image1.png”)。
My question concerns the syntax and security of how I used .load(): 我的问题与.load()的语法和安全性有关:
1) is this the proper syntax ? 1)这是正确的语法吗? On the jQuery page it is reversed, with the URL request within the .load() parentheses. 在jQuery页面上,URL要求相反,URL请求位于.load()括号内。 But the way I show above works for me. 但是我上面展示的方式对我有用。
2) according to the jQuery resource page, it says to pass into .load() just what you want to request from the server (example “/page.html”). 2)根据jQuery资源页面,它表示将您想要从服务器请求的内容传递到.load()中(例如“ /page.html”)。 Is it OK that I am passing in "a href" and "img" tags surrounding the Object's properties - or more specifically, that I am calling .load() on them ? 我可以在对象的属性周围传递“ a href”和“ img”标签吗?或者更确切地说,是在它们的属性上调用.load()吗? I imagine these are being passed back to the server via some behind-the-scenes jquery magic, so I want to make sure it is safe to use the .load() method in this manner. 我想这些是通过一些后台jquery魔术传递回服务器的,所以我想确保以这种方式使用.load()方法是安全的。
Thanks! 谢谢!
This is not the intended use of load
. 这不是load
的预期用途。 It "works" because what you've done would work without calling load at all, and load is likely failing silently because it's being called incorrectly (it expects a URL as a parameter) 之所以“有效”,是因为您所做的一切都可以在不调用load的情况下进行,并且由于调用不正确(它期望将URL作为参数),加载可能会以静默方式失败。
Just the first part, $("<a ... >")
will create the element out of the string provided. 仅第一部分, $("<a ... >")
将从提供的字符串中创建元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.