单击图像图标时将Class添加到特定DIV
我正在尝试为图片库中的缩略图实现一个名为 favorites (id="#favorites") 的 JqueryUI 按钮。该按钮可切换收藏夹图标 (span class="fav") 的显示。我使用同位素作为缩略图布局,它允许排序和过滤。我想做的是将一类“收藏夹”添加到同位素 DIV (class="item"),以便在单击单个缩略图的“收藏夹”图标时允许过滤图像。我还希望我的收藏夹图标粘在标记为收藏夹的每个拇指上,并且在单击 JqueryUi 按钮时选择后不会切换。 到目前为止,我能够使图标保持可见并将类添加到同位素 DIV,除了它将类添加到所有 DIV 以及所有最喜欢的图标保持可见之外。我需要让它只改变特定缩略图的特定 DIV 和最喜欢的图标。 我仍在学习 jquery,我的代码还很初级,对于这个目的可能是完全错误的。任何帮助或指导将不胜感激!
这是一个链接:图库 单击“Fav”按钮可切换图标
HTML:
<div class="item">
<a href="image.jpg" title="Image">
<img src="image.jpg" width="80" height="80" alt="Image" />
</a>
<span class="fav ui-icon ui-icon-heart"></span>
<div class="title">Image 1</div>
</div>
按钮代码:
// JQuery Ui button setup
$( "#favorites" ).button({
label: "Fav",
icons: { primary: "ui-icon-heart" }
});
// initially hides the icon
$('span.fav').hide();
//click function to toggle icon under thumbs
$("#favorites" ).click(function() {
$('span.fav').slideToggle('slow');
return false;
});
// my attempt at adding a class 'sticky' to icon to make it stay visible as well as add class '.favorites' to '.item' div to set it up for filtering
$("span.fav" ).live('click', function() {
$('.fav').addClass("sticky");
$('.fav').each(function ()
{
$(this).removeClass("fav");
});
$('.item').each(function ()
{
$(this).addClass("favorites");
});
return false;
});
我使用 .live,因为 Isotope 正在对代码进行动态更改,但这可能不是正确的做法。
I am trying to implement a JqueryUI button called favorites (id="#favorites") for thumbnails in an image gallery. The button toggles the display of the favorites icon (span class="fav"). I am using Isotope for the thumbnail layout which allows sorting and filtering. What I am trying to do is add a class of "favorites" to the Isotope DIV (class="item") to allow filtering of the images when the "fav" icon is clicked for an individual thumbnail. I also want my favorites icon to stick to each thumb that is marked as a favorite and not toggle after chosen when the JqueryUi button is clicked.
So far I am able to make the icons stay visible and add the class to the Isotope DIV except that it adds the class to all DIV's as well as all the favorite icons stay visible. I need to have it only alter the specific DIV and favorite icon of that particular thumbnail.
I am still learning jquery and my code is rudimentary and may be totally wrong for this purpose. Any help or direction would be appreciated!
Here is a link: Gallery
Clicking the "Fav" button toggles the icons
HTML:
<div class="item">
<a href="image.jpg" title="Image">
<img src="image.jpg" width="80" height="80" alt="Image" />
</a>
<span class="fav ui-icon ui-icon-heart"></span>
<div class="title">Image 1</div>
</div>
Button Code:
// JQuery Ui button setup
$( "#favorites" ).button({
label: "Fav",
icons: { primary: "ui-icon-heart" }
});
// initially hides the icon
$('span.fav').hide();
//click function to toggle icon under thumbs
$("#favorites" ).click(function() {
$('span.fav').slideToggle('slow');
return false;
});
// my attempt at adding a class 'sticky' to icon to make it stay visible as well as add class '.favorites' to '.item' div to set it up for filtering
$("span.fav" ).live('click', function() {
$('.fav').addClass("sticky");
$('.fav').each(function ()
{
$(this).removeClass("fav");
});
$('.item').each(function ()
{
$(this).addClass("favorites");
});
return false;
});
I used .live because Isotope is making dynamic changes to the code but this may be not the right thing to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,整个问题可能只是对您正在使用的 jQuery 函数的理解不正确。
问题在于您需要了解 this 变量,它将引用您正在交互的当前对象。对于“click()”方法,this 指的是被单击的元素。
代码的较短版本如下(也允许删除收藏夹):
这利用了 jQuery 的链接,允许您在某些内容上调用一个又一个的方法。注意只有一个“;”在函数中:)
(感谢您对您的内容进行格式化和评论。这样更容易理解和回答!)
To me it looks like the whole issue may be just an incorrect understanding of the jQuery functions you're using.
The problem lies simply that you need to be aware of the this variable, which in will reference the current object you're interacting with. In the case of the 'click()' method, this refers to the element that was clicked.
A shorter version of the code would be this (which also allows removing of favorites):
This takes advantage of jQuery's chaining, allowing you to call method after method on something. Notice only one ';' in the function :)
(Thanks for formatting and commenting your stuff. So much easier to understand and answer!)