简体   繁体   English

在图像上使用JQuery的.load()的正确方法是什么?

[英]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.

相关问题 将jQuery与“ use strict”结合使用的正确方法是什么? - What's the proper way to use jQuery in conjunction with 'use strict'? 正确的jQuery图片load()? - Proper jQuery image load()? 加载正确的PhoneGap文件的最佳方法是什么 - What's the best way to load the proper PhoneGap file 使用引导程序对图像进行居中和裁剪的正确方法是什么(如果需要,可以使用jquery) - What is the proper way to center and crop an image using bootstrap (with jquery if needed) 在javascript中测试对象是否是jQuery对象的正确/正确方法是什么? - What's the correct/proper way to test if an object is a jQuery object in javascript? <a>用Javascript / JQuery</a>替换` <a>`</a>的正确方法是<a>什么?</a> - What's the proper way to replace `<a>` using Javascript/JQuery? 使用jQuery检查元素是否具有ID的正确方法是什么? - What's the proper way to check if an element has an ID or not with jQuery? Javascript / jQuery变量,使用var的正确方法是什么? - Javascript/jQuery variable, what is the proper way to use a var? 在jQuery中将子选择器与上下文节点一起使用的新方法是什么? - What is the new proper way to use a child selector with a context node in jQuery? 在 angular js 指令中使用 jquery 的正确方法是什么? - what is proper way to use jquery in angular js directives?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM