简体   繁体   English

Jquery Noobish帮助:Img OnClick

[英]Jquery Noobish Help: Img OnClick

I'm very new to JQuery, and I'm having some trouble understanding how to do this: 我是JQuery的新手,我在理解如何执行此操作时遇到了一些麻烦:

I have an image of class "imgexpander" with the src attribute set to "img1.png". 我有一个类“imgexpander”的图像,src属性设置为“img1.png”。 When the image is clicked on, it should look to see whether a div with class "expand" is currently hidden or visible. 单击图像时,它应该查看具有“expand”类的div当前是隐藏还是可见。

  • If it's hidden (which is the default), it shows it (I know how to use show()) and changes the src attribute of the image to "img2.png". 如果它是隐藏的(这是默认值),它会显示它(我知道如何使用show())并将图像的src属性更改为“img2.png”。

OR: 要么:

  • If the div is visible, it hides it and changes the src attribute of the image to "img1.png". 如果div是可见的,它会隐藏它并将图像的src属性更改为“img1.png”。

I'm not familiar with the available functions in JQuery yet. 我还不熟悉JQuery中的可用功能。 How could this be accomplished? 怎么可以实现呢? Can you give me some sample code that I can work with? 你能给我一些我可以使用的示例代码吗?

Thanks in advance! 提前致谢!

UPDATE : I forgot to add a detail: is it possible to somehow make the onclick of an image of class "imgexpander" only influence divs that are all included together in one big div? 更新 :我忘了添加一个细节:是否有可能以某种方式使onclick类的图像“imgexpander”只影响所有包含在一个大div中的div? So, the hierarchy would be something like: 因此,层次结构将类似于:

  • big div 大div
    • image with onclick onclick的图像
    • div that needs to be influenced 需要受影响的div
  • another big div 另一个大的div
    • image with onclick onclick的图像
    • div that needs to be influenced 需要受影响的div

The desired result would be to have each "image with onclick" only influence "divs that need to be influenced" inside its respective "big div". 期望的结果是使每个“带有onclick的图像”仅影响其各自“大div”内的“需要受影响的div”。 Is this possible? 这可能吗? I'm not sure the current answer would fit, but thanks! 我不确定目前的答案是否合适,但谢谢!

How about: 怎么样:

$("img.imgexpander").click
(
  function()
  {        
    var expandableDIVs = $(this)
                           .parents("div.bigdiv:first")
                           .find("div.expand");    
    expandableDIVs.toggle();
    this.src = expandableDIVs.is(":visible") ? "img2.png" : "img1.png";
  }
);

This code sets up a click event handler for all IMG elements of class imgexpander. 此代码为类imgexpander的所有IMG元素设置单击事件处理程序。 The handler toggles the visibility of all DIV elements of class expand. 处理程序切换类expand的所有DIV元素的可见性。 The src attribute of the image is updated by testing if any of the DIV elements matched by "div.expand" are visible. 通过测试是否可以看到与“div.expand”匹配的任何DIV元素来更新图像的src属性。

Notice that I can assign the jQuery wrapped set of DIVs matching the "div.expand" selector to a JavaScript variable for later use. 请注意,我可以将与“div.expand”选择器匹配的jQuery包装的DIV集合分配给JavaScript变量以供以后使用。

The this keyword in the event handler refers to the current DOM element matched by the "img.imgexpander" selector. 事件处理程序中的this关键字引用与“img.imgexpander”选择器匹配的当前DOM元素。 Remember, there can be several elements matched by this expression. 请记住,此表达式可以匹配多个元素。

EDIT : The method of acquiring the div.expand elements has been updated to reflect the changes to the OP. 编辑 :获取div.expand元素的方法已更新,以反映OP的更改。 Only DIV elements that are children of the parent bigdiv classed DIV will be toggled when an img is clicked. 当点击img时,只有作为父bigdiv分类DIV的子元素的DIV元素才会被切换。

Note that care has been taken to ignore the way that elements are marked up. 请注意,已经注意忽略元素标记的方式。 It is important to us that the IMG element has a parent DIV somewhere in its parent chain of class bigdiv, but this element does not have to be an immediate parent. 对我们来说重要的是,IMG元素在其bigdiv类的父链中的某处具有父DIV,但是该元素不必是直接父元素。 This is the reason for my use of the :first pseudo-selector. 这就是我使用:first伪选择器的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM