简体   繁体   English

jQuery onClick for<li> 元素不是函数

[英]jQuery onClick for <li> element not a function

Strangely, the following code throws a jQuery.Deferred exception:奇怪的是,下面的代码抛出了一个 jQuery.Deferred 异常:

var miscUls = $("#miscellaneous").find("ul");
miscUls.children('li')[0].onClick(function() {
    modal.style.display = "none";
});

...and... ...和...

miscUls.children().first().onClick(....

...and ...和

miscUls.children('li').eq(0).onClick(....

not quite sure how to stylize the selector, I'm merely trying to attach an onClick() event handler to the first element in that unordered list.不太确定如何风格化选择器,我只是想将 onClick() 事件处理程序附加到该无序列表中的第一个元素。 I have also tried to assign a unique id to that element, and select it via也试过一个唯一的ID分配给该元素,并通过选择它

miscUls.getElementById("newUser").onClick(....

to no avail.无济于事。 Any help with this seemingly simple problem would be greatly appreciated.对这个看似简单的问题的任何帮助将不胜感激。 Thanks.谢谢。

try :first-child selector on li like thisli上尝试:first-child选择器,就像这样

$("#miscellaneous ul li:first-child").click(function(){
        // your code
    })

Jquery's click event helper is click not onClick Jquery 的点击事件助手是点击而不是 onClick

var miscUls = $("#miscellaneous").find("ul");
miscUls.children('li')[0].click(function() {
    modal.style.display = "none";
});

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

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