开发者

Jquery - selector, made of object and string, can it be done?

Assume that there is an object, passed as an argument to a function. the argument name is "obj". can it be concatenated as followed?

$(obj + " .className")....开发者_运维百科..

OR

$(obj + "[name='obj_name'])......

Thanks.


No, but you can use the filter() method to filter out the object itself:

$(obj).filter('.className')...
$(obj).filter('[name=obj_name]')...

Or, if you want to find the children with those qualities:

$(obj).find('.className')...
$(obj).find('[name=obj_name]')...

Or, an alternative syntax to find, giving the obj as the context to the $() function:

$('.className', obj)...
$('[name=obj_name]', obj)...


The second argument of your selector is the context:

$(".className", obj).each(...);

This will restrict all matches to obj. So assuming obj is a reference to div.parent:

<div class="parent">
  <p class="className">I'll be found</p>
</div>
<p class="className">I will NOT be found</p>


$(obj.tagName + " .className")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜