简体   繁体   中英

jQuery onClick for <li> element not a function

Strangely, the following code throws a jQuery.Deferred exception:

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. I have also tried to assign a unique id to that element, and select it via

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 this

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

Jquery's click event helper is click not onClick

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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