jquery selector not working, why?

前端 未结 8 624
挽巷
挽巷 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:44

    $("#content") returns the jQuery collection containing the elmenent with id content. id is an attribute of an element, not a property of the collection. You would get the element's ID by chaining the attr() method off the collection:

    alert($("#content").attr('id'));
    

    Or, when eaching the collection, inside the function passed to each this will be the actual element, so you could access this.id

    $("#content").each(function () {alert(this.id);});
    

提交回复
热议问题