javascript 将点击事件添加到 Jquery UI 手风琴标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4492467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:53:04  来源:igfitidea点击:

add click event to Jquery UI accordian Header

javascriptjqueryhtmljquery-ui

提问by kralco626

When you create a Jquery UI accordian you get a bunch of headers, that when you click them a div opens. However, I would like to preform an additional action upon clicking the header.

当你创建一个 Jquery UI 手风琴时,你会得到一堆标题,当你点击它们时会打开一个 div。但是,我想在单击标题时执行附加操作。

How do i do this?

我该怎么做呢?

Any ideas?

有任何想法吗?

Thanks!

谢谢!

回答by kralco626

Javascript

Javascript

$("#CreateNewUserHeader").click(function() {
    alert("test");
});

html

html

<h3 id = "CreateNewUserHeader"><a >Create New User</a></h3>
<div>some stuff</div>

回答by Sarfraz

You need to wrap your code in readyhandler:

您需要将代码包装在ready处理程序中:

$(function(){
  $("#CreateNewUserHeader").click(function() {
    alert("test");
  });
});

Also make sure that you do not assign same idto more than one elements.

还要确保不要将相同的 id分配给多个元素。

回答by PriorityMark

What I've done when I needed to do this in the past was something like this:

过去当我需要这样做时,我所做的是这样的:

$('#accordion h3 a').bind('click', function (e) {
  // bind to the the header / anchor clicks
  if (!condition) {
    e.preventDefault();
    e.stopPropagation();
  }
});

回答by Tristanisginger

You guys should read the documentation, there is an event handler that will do it all for you.

你们应该阅读文档,有一个事件处理程序可以为您完成所有工作。

https://api.jqueryui.com/accordion/#event-activate

https://api.jqueryui.com/accordion/#event-activate

$( ".selector" ).accordion({
  activate: function( event, ui ) {}
});