开发者

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

开发者 https://www.devze.com 2022-12-15 02:51 出处:网络
Assume that there is an object, passed as an argument to a function. the argument name is \"obj\". can it be concatenated as followed?

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

精彩评论

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