JQuery $(this) selector function and limitations

后端 未结 5 1145
南方客
南方客 2020-12-01 08:31

I need help understanding $(this). Is it possible to narrow the focus of \'this\' within the parentheses or does \"this\" preclude the use of any other attributes?

5条回答
  •  一生所求
    2020-12-01 09:23

    You can take a look at this: http://api.jquery.com/each/

    Basically this is the DOM element that the callback is referencing. You can't do $(this + " div") because you are adding a string to an element. You first need to get the id like in your third code snippet.

    By the way, $(this).children("div") is not the same as $('#'+$(this).attr("id")+" div")

    First off, this might not have an ID, in which case the second way won't work at all. And even if it does have an ID, that second code is actualy $(this).find("div"). If you want children only you have to do:

    $('#'+$(this).attr("id") + " > div")
    

提交回复
热议问题