简体   繁体   English

如何使div在jQuery滑动菜单上可点击

[英]How to make divs clickable on a jQuery sliding menu

I have a sliding div menu made with jQuery, but you have to click on the LINK within the Div in order to make it slide. 我有一个使用jQuery制作的滑动div菜单,但是您必须单击Div内的LINK才能使其滑动。 Can anyone tell me how to make the div ITSELF clickable? 谁能告诉我如何使div ITSELF可点击?

You can see the menu and code here: http://jsfiddle.net/WhfDc/ 您可以在此处查看菜单和代码: http : //jsfiddle.net/WhfDc/

I've tried to use CSS to make the anchor cover the entire div and it works sometimes. 我试图使用CSS来使锚覆盖整个div,并且有时可以正常工作。 Any idea whether this is correct or not? 知道这是否正确吗?

Yes, it is possible by attaching click event handler on the div instead of anchor element which is inside the div. 是的,可以通过在div上而不是在div内的锚元素上附加click事件处理程序来实现。

Also I would suggest you to use jQuery to attach event handler instead of inline event handler. 我也建议您使用jQuery附加事件处理程序,而不是内联事件处理程序。

Working demo - http://jsfiddle.net/WhfDc/1/ 工作演示-http: //jsfiddle.net/WhfDc/1/

You don't need all that Javascript. 您不需要所有的Javascript。 You can just bind the menu click event (for all divs with .evtog classes), the handler hides the submenus that are visibles and shows the submenu. 您可以绑定菜单click事件(对于所有具有.evtog类的div),处理程序将隐藏可见的子菜单并显示该子菜单。

Try this: 尝试这个:

$(function() {
  $("div.evtog").click(function() {
    $("div[name=events]:visible").slideUp(600);
    $(this).next().slideDown(200);
  })
});​

Note the $() wrapping the function, this is needed because in order to bind the elements they must be on DOM. 注意$()包装函数,这是必需的,因为要绑定元素,它们必须在DOM上。 And this guarantees that the function will be executed when the DOM is ready. 并且这保证了在DOM准备就绪时将执行该功能。

Working example: JsFiddle 工作示例: JsFiddle

More info: 更多信息:

.click .click
:visible :可见
.next 。下一个
.ready 。准备

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

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