JQuery methods and DOM properties

后端 未结 4 886
梦如初夏
梦如初夏 2021-01-31 12:06

I am confused as to when I can use the DOM properties and when I could use the Jquery methods on a Jquery object. Say, I use a selector

var $elemSel = $(\'#myDi         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 12:35

    Because there can be more than one match in jQuery selectors you need to get the elements out of the "array".

    You can use the get method to return a single DOM element or an array or DOM elements.

    eg:

    var elims = $("div").get();
    for(var i in elims)
        alert(elims[i].nodeName);
    

    Edit:

    $elemSel.get(0).is(':checked') will not work because you are getting the Dom object and then trying to use jQuery functions on it. If you want to get a single jQuery element use eq.

    $elemSel.eq(0).is(':checked');
    

提交回复
热议问题