jQuery.each() 通过 php 输出进行迭代
我是 jQuery 的新手,但有一个问题,希望你们中的一些人能帮忙解决...我已经在这个网站上尝试了几个例子,但似乎无法找到一个可靠的解决方案。每个链接都是完全唯一的 #id。我希望迭代生成的(php)id 并附加一个通用的 .click() 事件。我正在使用云缩放(jQuery)。更棒的是,当我使用“.tagImg”作为类选择器时,它将在新窗口中打开图像(链接)。但是当我指定特定的 id(#tagImage-0) 时,脚本可以正常工作。因此,我需要一种方法来迭代未知数量的图像,以使其正常工作。
极其基本的骨架:
<div class="tagImg">
<a id="tagImage-0" href="big0.img"><img src="/images/thumb0.jpg"></a>
<a id="tagImage-1" href="big1.img"><img src="/images/thumb1.jpg"></a>
<a id="tagImage-2" href="big2.img"><img src="/images/thumb2.jpg"></a>
<a id="tagImage-3" href="big3.img"><img src="/images/thumb3.jpg"></a>
<a id="tagImage-4" href="big4.img"><img src="/images/thumb4.jpg"></a>
<a id="tagImage-5" href="big5.img"><img src="/images/thumb5.jpg"></a>
</div>
每个链接都有效:
$("#tagImage-0").click(function() {
// Process some code
});
在新窗口中打开链接(请记住,使用云缩放):
$(".tagImg").click(function() {
// Process some code
}):
使用了这个但不起作用?
$("#tagImage > a").click(function() {
// Process some code
});
:如果需要,我可以提供一些更具体的代码..
I am new to jQuery but have a question I hope some of you can help with... I have tried several examples on this site but can't seem to figure out a solid solution. Each link is completely unique by #id. I am looking to iterate through the generated(php) ids and append a universal .click() event. I am using cloud-zoom (jQuery). The kicker is that when I use ".tagImg" as the class selector, it will open the image(link) in a new window. But when I specify a specific id(#tagImage-0) the script works perfectly. Therefore I need a way to iterate through the unknown amount of images to make this work correctly.
Extremely basic skeleton:
<div class="tagImg">
<a id="tagImage-0" href="big0.img"><img src="/images/thumb0.jpg"></a>
<a id="tagImage-1" href="big1.img"><img src="/images/thumb1.jpg"></a>
<a id="tagImage-2" href="big2.img"><img src="/images/thumb2.jpg"></a>
<a id="tagImage-3" href="big3.img"><img src="/images/thumb3.jpg"></a>
<a id="tagImage-4" href="big4.img"><img src="/images/thumb4.jpg"></a>
<a id="tagImage-5" href="big5.img"><img src="/images/thumb5.jpg"></a>
</div>
Works per link:
$("#tagImage-0").click(function() {
// Process some code
});
Opens link in new window (keep in mind, using cloud-zoom):
$(".tagImg").click(function() {
// Process some code
}):
Used this but did not work?:
$("#tagImage > a").click(function() {
// Process some code
});
If required, I can provide some more specific code..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选择器
#tagImage > a
表示:元素#tagImage
的所有直接后代,它们也是锚点。没有任何元素可以与该描述匹配。我认为你的意思是:基本上,你有一个错字:)
The selector
#tagImage > a
means: all immediate descendants of element#tagImage
which are also anchors. No elements can match that description. I think what you meant to do was:Basically, you had a typo :)
你能尝试一下
上面的做法是引用 div 元素上引用的“tagImg”类内部的“a”元素吗?
Can you try
What the above does is reference the "a" elements inside of the "tagImg" class referenced on the div element.
尝试:
Try: