jQuery“for”循环(jQuery “for” loop)
我想在这里写一些非常基本的jQuery代码。
我在这里做的是我希望图像在悬停时切换。 我有5个图像,当我编写5个代码时,我可以将其归档,每个图像1个。
$('#menu > ul:first-child > li:nth-child(1)').hover(function(){ $('li:nth-child(1) > .image-hover').toggle(); $('li:nth-child(1) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(2)').hover(function(){ $('li:nth-child(2) > .image-hover').toggle(); $('li:nth-child(2) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(3)').hover(function(){ $('li:nth-child(3) > .image-hover').toggle(); $('li:nth-child(3) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(4)').hover(function(){ $('li:nth-child(4) > .image-hover').toggle(); $('li:nth-child(4) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(5)').hover(function(){ $('li:nth-child(5) > .image-hover').toggle(); $('li:nth-child(5) > .image').toggle(); });
现在我想缩短代码,使用“for”循环。 我的想法是我有一个变量i,从1开始,将1加1直到5.上面的第n个孩子,而不是具体的数字,现在将成立i。 当我改变时,第n个孩子(i)将具有i的相应值。
(function($){ for (var i = 1; i < 6; i++) { $('#menu > ul:first-child > li:nth-child(i)').hover(function(){ $('li:nth-child(i) > .image-hover').toggle(); $('li:nth-child(i) > .image').toggle(); }); }; })(jQuery);
尽管这个缩短的代码不起作用。
有人可以帮我弄这个吗? 非常感谢。
I am trying to write some very basic jQuery code here.
What I am doing here is I want the image to toggle when hover. I have 5 images, and I was able to archive that when I wrote 5 codes, 1 for each image.
$('#menu > ul:first-child > li:nth-child(1)').hover(function(){ $('li:nth-child(1) > .image-hover').toggle(); $('li:nth-child(1) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(2)').hover(function(){ $('li:nth-child(2) > .image-hover').toggle(); $('li:nth-child(2) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(3)').hover(function(){ $('li:nth-child(3) > .image-hover').toggle(); $('li:nth-child(3) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(4)').hover(function(){ $('li:nth-child(4) > .image-hover').toggle(); $('li:nth-child(4) > .image').toggle(); }); $('#menu > ul:first-child > li:nth-child(5)').hover(function(){ $('li:nth-child(5) > .image-hover').toggle(); $('li:nth-child(5) > .image').toggle(); });
Now I want to shorten the code, using the "for" loop. The idea is I have a variable i, starting from 1, increasing 1 by 1 up to 5. The nth-child above, instead of having a specific number, will now hold i. As i changes, nth-child(i) will have respective values of i.
(function($){ for (var i = 1; i < 6; i++) { $('#menu > ul:first-child > li:nth-child(i)').hover(function(){ $('li:nth-child(i) > .image-hover').toggle(); $('li:nth-child(i) > .image').toggle(); }); }; })(jQuery);
This shorten code doesn't work though.
Can someone help me with this? Thanks a lot.
原文:https://stackoverflow.com/questions/23902052
最满意答案
上次我检查它是Flash。 它使用ActionScripts FileReference类。 如果要将此类功能添加到您自己的应用程序中,请查看http://swfupload.org/ 。
一开始最令我惊讶的是,你可以拥有这种功能,没有任何消息要求用户许可。 我已经习惯了这种功能的java applet,并且要访问需要签名的文件系统,并在执行前请求用户权限。 Flash的诀窍是你只需:
- 打开FileDialog以选择文件
- 您可以对所选文件执行的唯一操作是上传
因此,用户没有安全风险,因为程序员永远无法访问文件系统。
Last time I checked it was Flash. It uses ActionScripts FileReference class. Check out http://swfupload.org/ if you want to add this kind of functionality to your own application.
What surprised me the most in the beginning was that you could have this kind of functionality with no message asking for user permission whatsoever. I was used to java applets for this kind of feature, and to access the file system they need to be signed, and ask for user permission before execution. The trick with Flash is that you just :
- open a FileDialog to select files
- the only thing you can do with the selected files is uploading
So there is no security risk for the user, as the programmer never gets access to the file system.
相关问答
更多-
回答是因为我无法发表评论 忍不住注意到可能导致问题的额外时间段: vvvvvvvv Form->input('files.', array('type' => 'file', 'multiple' => 'multiple', 'label' => '')); ?> ^^^^^^^^ 然后你将循环使用$this->request->data['Uplo ...
-
您只能获得引用,而不是有效的文件URL,这对AssetsLibrary之外的类没用。 但是,您可以非常轻松地获取资产数据并将其持久保存到磁盘: - (void)writeAsset:(ALAsset *)asset toPath:(NSString *)path { ALAssetRepresentation *representation = asset.defaultRepresentation; long long size = representation.size; NSM ...
-
你需要提供bytes ,但是你提供了str 。 您可以通过将文件模式更改为二进制来获取bytes 。 即,而不是: f = open(sourcefile, "r") 做: f = open(sourcefile, "rb") You need to supply bytes, but you're supplying str. You can get bytes by changing the file mode to binary. I.e., instead of: f = open(source ...
-
您需要确保/tmp文件夹可由Web服务器用户写入。 我在猜它的Apache? 您需要检查是否有www-data (例如ubuntu上的默认apache用户)。 问题> chown -R www-data:www-data /tmp或issue> sudo chmod 777 /tmp 。 如果您不想篡改/tmp linux文件夹,可以在php.ini config http://php.net/manual/en/ini.core.php#中的app文件夹中设置自己的custom /tmp文件夹。 ini. ...
-
通过保管箱选择器选择文件将生成该文件的URL。 Fine Uploader期望此时每个请求都会发送一个实际文件。 如果您希望与该Dropbox选择器API集成,或者能够发送URL代替实际文件,则需要在Github项目的问题跟踪器中打开功能请求 。 Selecting a file via the dropbox chooser will result in a URL to that file. Fine Uploader expects an actual file to be sent with eac ...
-
上次我检查它是Flash。 它使用ActionScripts FileReference类。 如果要将此类功能添加到您自己的应用程序中,请查看http://swfupload.org/ 。 一开始最令我惊讶的是,你可以拥有这种功能,没有任何消息要求用户许可。 我已经习惯了这种功能的java applet,并且要访问需要签名的文件系统,并在执行前请求用户权限。 Flash的诀窍是你只需: 打开FileDialog以选择文件 您可以对所选文件执行的唯一操作是上传 因此,用户没有安全风险,因为程序员永远无法访问文 ...
-
如何使用Javascript将文件的各部分并行上传到Dropbox(How to do Parallel upload of parts of a file to dropbox using Javascript)[2022-02-13]
如果你指的是dropbox-sdk-js ,你可以使用上传会话上传一个大文件: filesUploadSessionStart filesUploadSessionAppendV2 filesUploadSessionFinish 您可以使用上传会话并行上传多个不同的文件,但只能串行上传单个文件的数据。 也就是说,对于单个文件,您一次只能上传一个连续的数据。 If you're referring to dropbox-sdk-js, you can upload a large file using up ... -
它与.NET 4.5没有任何关系,它是您选择的项目类型的问题。 它选择了.NETCore配置文件,即从可移植类库项目或Windows应用商店项目获得的配置文件。 您尝试使用的库只能在桌面应用程序中使用。 您可以使用他们的通用REST API 。 如果这是一个商店应用程序Fuggedaboudit,当用户感到无聊等待时,您无法保持文件传输。 It doesn't have anything to do with .NET 4.5, it is the project type you selected tha ...
-
在主类中移动for循环: Log.i("toBeUploaded", " " + Arrays.toString(toBeUploaded)); Upload upload = new Upload(CalcDBActivity.this, mDBApi, getIntent().getExtras().getString("project") + File.separatorChar, toBeUploaded); upload.execute(); 进入上传课程: p ...
-
如何使用dropbox java api同时上传多个文件(How to upload multiple files at the same time using the dropbox java api)[2022-02-24]
是的,您可以使用多个线程调用API并上传文件。 您可以使用线程池 。 您需要确定创建不会影响性能的线程数的要点。 下面的代码将允许您在5个单独的线程中上传10个文件(在fileLocations数组中提供)。 public class UploadThread implements Runnable { private String fileLocation; public UploadThread(String s){ this.fileLocation=s; ...