How to do this using jQuery - document.getElementById(“selectlist”).value

前端 未结 6 2039
天涯浪人
天涯浪人 2020-12-12 21:48

In jQuery, what is the equivalent to document.getElementById(\"selectlist\").value ?

I am trying to get the value of a select list item.

Thanks.

6条回答
  •  一个人的身影
    2020-12-12 22:12

    For those wondering if jQuery id selectors are slower than document.getElementById, the answer is yes, but not because of the preconception that it searches through the entire DOM looking for an element. jQuery does actually use the native method. It's actually because jQuery uses a regular expression first to separate out strings in the selector to check by, and of course running the constructor:

    rquickExpr = /^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/
    

    Whereas using a DOM element as an argument returns immediately with 'this'.

    So this:

    $(document.getElementById('blah')).doSomething();
    

    Will always be faster than this:

    $('#blah').doSomething();
    

提交回复
热议问题