How to get a DOM Element from a JQuery Selector

前端 未结 4 940
不思量自难忘°
不思量自难忘° 2020-11-22 13:26

I\'m having an impossibly hard time finding out to get the actual DOMElement from a jquery selector. Sample Code:



        
4条回答
  •  生来不讨喜
    2020-11-22 13:31

    If you need to interact directly with the DOM element, why not just use document.getElementById since, if you are trying to interact with a specific element you will probably know the id, as assuming that the classname is on only one element or some other option tends to be risky.

    But, I tend to agree with the others, that in most cases you should learn to do what you need using what jQuery gives you, as it is very flexible.

    UPDATE: Based on a comment: Here is a post with a nice explanation: http://www.mail-archive.com/jquery-en@googlegroups.com/msg04461.html

    $(this).attr("checked") ? $(this).val() : 0
    

    This will return the value if it's checked, or 0 if it's not.

    $(this).val() is just reaching into the dom and getting the attribute "value" of the element, whether or not it's checked.

提交回复
热议问题