开发者

What does this mean in jquery $('#id', javascript_object);

开发者 https://www.devze.com 2023-01-08 02:12 出处:网络
what does this $(\'#id\', javascript_object); mean in jquery . I know that we can pass a context in the second parameter to the selector . But what happens when the second parameter is a javascript ob

what does this $('#id', javascript_object); mean in jquery . I know that we can pass a context in the second parameter to the selector . But what happens when the second parameter is a javascript object 开发者_StackOverflow中文版.

Thanks in advance for the help .


The second parameter is the selector context. It restricts searching to a specific document object or the children of a certain DOM element.

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, if within a callback function we wish to do a search for an element, we can restrict that search:

$('div.foo').click(function() {
  $('span', this).addClass('bar');
});

Since we've restricted the span selector to the context of this, only spans within the clicked element will get the additional class.

In the example you show, you would be searching for the element with the ID id among the children of javascript_object.

0

精彩评论

暂无评论...
验证码 换一张
取 消