jquery selector not working, why?

前端 未结 8 623
挽巷
挽巷 2021-01-18 09:29

I simply want to manipulate the elements within the #content div, but $(\'#content\') doesn\'t seem to work, and yet in other places it does! My relevant code i

8条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 09:57

    There is a difference between a DOM element and a jQuery selection that contains a DOM element. A jQuery selection is a wrapper around the DOM object to make it easier to use. It doesn't have an id property, but a DOM element does. On the other hand, jQuery does provide a way to access elements' properties using the attr method:

    document.getElementById('content').id // access the id property of a DOM element
    $('#content').attr('id')              // use jQuery to select the element and access the property
    

    The length property, however, is provided by a jQuery selection, which is why that works fine for you.

提交回复
热议问题