转入内联OnClick以单击Jquery [英] Turn Inline OnClick to Click Jquery

查看:64
本文介绍了转入内联OnClick以单击Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单列表的内联样式。我不知道如何使用jQuery进行点击。

I have the following inline styles for a dropdown menu list. I do not know how to make it in to 'click' using jQuery.

<button class="btn btn-primary mw" data-toggle="dropdown"><span class="caret">Subject</span></button>
  
  <div class="dropdown-menu">

     <a class="dropdown-item Business" onclick="selectBusiness()"> Business</a>
     <a class="dropdown-item ICT" onclick="selectICT()"> ICT</a>
     <a class="dropdown-item Arts" onclick="selectArts()"> Arts</a>

  </div>

更多信息:

我尝试按下按钮代码执行此操作,并且工作正常。但正如我所说,我不知道如何让它适用于下拉菜单。

I tried to do this following code for a button and it's working fine. But as I said I do not know how to make it work for a dropdown menu.

var btn = document.querySelector("button.selectAll");
btn.addEventListener("click", selectAll);
....

您的输入将不胜感激!

推荐答案

如果您想使用jQuery,您可以将侦听器添加到每个类的<您的下拉列表中有code>< a> 。

If you want to use jQuery, you can add listener to the classes of each <a> inside your dropdown.

$(".Business").on('click', function() {
//selectBusiness() or your code here
});

$(".Arts").on('click', function() {
//selectarts() or your code here
});

$(".ICT").on('click', function() {
//selectICT() or your code here
});

另一个解决方案是为所有下拉项添加一个监听器,然后检查内容选定的一个。

Another solution would be to add a single listener for all your dropdown-items and then checking the content of the selected one.

$(".dropdown-item").on('click', function() {
    switch ($(this).text()) {
        case "Business": 
        selectBusiness();
        break;


    }

});

这篇关于转入内联OnClick以单击Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆