为了账号安全,请及时绑定邮箱和手机立即绑定

jQuery选择器中的“上下文”是什么?

jQuery选择器中的“上下文”是什么?

达令说 2019-09-21 15:28:29
之间有什么区别$('input.current_title', '#storePreferences').prop('disabled', false);和$('#storePreferences input.current_title').prop('disabled', false);?
查看完整描述

2 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

有什么区别$('input.current_title', '#storePreferences').prop('disabled', false);和$('#storePreferences input.current_title').prop('disabled', false);?


是的,但是很微妙

区别在于如何选择元素。


$('input.current_title', '#storePreferences');

等于1:


$('#storePreferences').find('input.current_title');

但不等同于:


$('#storePreferences input.current_title');

即使相同的元素也会受到影响。


它们之所以不同,是因为using find允许将上下文返回到#storePreferenceswhen end调用。


1:jQuery v1.9.1源代码中的194-202行

// HANDLE: $(expr, $(...))

} else if ( !context || context.jquery ) {

    return ( context || rootjQuery ).find( selector );


// HANDLE: $(expr, context)

// (which is just equivalent to: $(context).find(expr)

} else {

    return this.constructor( context ).find( selector );

}

根据您的问题,将修改相同的元素,因此功能上没有差异,但重要的是要意识到所使用选择器的广泛含义。


查看完整回答
反对 回复 2019-09-21
  • 2 回答
  • 0 关注
  • 643 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信