简体   繁体   English

jQuery切换图片点击

[英]Jquery Toggle on click of image

I am trying to understand how the jquery toggle works. 我试图了解jquery切换的工作方式。 I want to toggle to next anchor with class plr-anchor on click of image with class go_down . 我想在单击类go_down的图像时切换到具有plr-anchor类的下一个锚点。 The data is populated using maps. 使用地图填充数据。

Jquery code: jQuery代码:

$('.go_down').on('click',function(e){
    #('.plr-anchor').next('.plr-anchor').toggle()});
}

Code snippet: 程式码片段:

{data.map(function(categ) {
return <div>
<a className="plr-anchor" id={categ.short_name}></a>
<img src="/static/img/icon_up.png" className="go_down"/>
</div>}.bind(this)}

There seems to be a problem with the syntax on the Jquery call function. Jquery调用函数的语法似乎存在问题。 I am newbie with Jquery, any help will be great. 我是Jquery的新手,任何帮助都会很棒。 Thanks in advance. 提前致谢。

Your jQuery code has a "#" instead of a "$". 您的jQuery代码使用“#”而不是“ $”。 By the way, React and jQuery don't always go together. 顺便说一下,R​​eact和jQuery并不总是在一起。 The whole purpose of React is to use virtual dom and let React take care of dom updates. React的整个目的是使用虚拟dom,并让React负责dom更新。 While jQuery is mostly about direct dom manipulation. jQuery主要是关于直接dom操作的。 What you are trying here will interfere with React because it won't expect the dom to change without the v-dom changing. 您在这里尝试的操作会干扰React,因为它不会期望v-dom发生更改而更改dom。

Ideally, you should be using event handlers and state in your component to toggle visibility. 理想情况下,您应该在组件中使用事件处理程序和状态来切换可见性。

You have used # instead of $ inside click handler. 您在点击处理程序中使用#代替了$ Also you need to find exact plr-anchor which is before the clicked image. 另外,您还需要找到位于单击图像之前的精确plr-anchor Right now you are toggling all plr-anchor . 现在,您正在切换所有plr-anchor

For dynamic elements, use $(document).on(event, selector, function(){}); 对于动态元素,请使用$(document).on(event, selector, function(){}); for binding click handlers. 用于绑定点击处理程序。 See below code 见下面的代码

$(function(){
  $(document).on('click','.go_down',function(e){
     $(this).prev('.plr-anchor').toggle();
  });
});

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

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