javascript - 如何给 ul 标签元素添加 onchange 事件?

标签 javascript jquery html

我有一个像这样的“ul”和“li”集合的下拉菜单,

<div style="float:left;">
    <div class="group">
        <div style="width:80px;">Dropdown<i class="fa fa-caret-down"></i></div>
            <ul style="float:left;">
                <li><a href="javascript:void(0);">Option1</a></li>
                <li><a href="javascript:void(0);">Option2</a></li>
                <li><a href="javascript:void(0);">Option3</a></li>
            </ul>
    </div>
</div>

我想将 onchange() 事件添加到 ul 标签。我可以说 onchange 我必须调用一个函数 SelectedItem(optionselected) 并将选定的“li”文本作为参数。任何人都请帮助我。谢谢。

最佳答案

The change event is sent to an element when its value changes. This event is limited to elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

参见 http://api.jquery.com/change/

所以 .change()不适用于无序列表...

使用 click事件

    $('li').click(function(){
     //your code
 });

关于javascript - 如何给 ul 标签元素添加 onchange 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25520986/

相关文章:

javascript - 告诉表列的数量并将该数量作为顶部 colspan

jquery - 如何将键和值从一个对象复制到另一个对象而不复制值

jQuery addclass/remove Class 工作一次

javascript - TinyMCE 4.7.4 不适用于包含查询字符串参数的 URL

javascript - 如何使用 'src' 更改一些 <img> 标签的 'for loop' 值?

javascript - JavaScript : Accessing another method of the same object?

javascript - 通过在 for 循环中解析字符串来委托(delegate)事件

javascript - NWjs : Chat head click not working if -webkit-app-region: drag is set

javascript - 如果 div 可见,则不执行任何操作,否则 .show

javascript - 如何使用 JavaScript 更改基于 onclick 的文本?