JQuery selectors for first li

前端 未结 4 414
旧时难觅i
旧时难觅i 2021-01-27 19:38

I need an onclick event, when the user clicks on the first li aka(Any Date).How do I select that element using jQuery?

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-27 19:52

    You can use jquery :first selector to target the 1st element in a collection of elements.

    $('ul li:first').click(function()
    {
       // do something 
    });
    

    Example : https://jsfiddle.net/3mb8ep19/

    or jquery first() filter :

    $('ul li').first().click(function()
    {
       // do something
    });
    

    Example : https://jsfiddle.net/3mb8ep19/1/

提交回复
热议问题