Is there such thing as a relative jQuery selector?

随声附和 提交于 2019-12-03 10:37:17

You can start with a child selector (>) when using .find() as well, like this:

$(this).find('> table > tbody > tr > td')

It's an often overlooked use case, but it works just great for what you're after.

As Nick said, you can use find(), or you can use selector context:

$('> table > tbody > tr > td', this)

// Is the equivalent of
$(this).find('> table > tbody > tr > td')

An alternative way would be passing a second parameter $('selector', context), which defines a context for search.

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.

$( "div.foo" ).click(function() {
    $( "span", this ).addClass( "bar" );
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!