如何搜索所有<输入>DOM 元素的后代并禁用它们输入>
假设我有一个 DOM 对象:
var a = document.getElementById('parent')
我想搜索元素 a
内的所有 input
。
我应该在 jQuery 中做什么?
我想禁用 a
内的所有输入,如下面的语法:
$('#parent input').attr('disabled',true);
我尝试过
$(a).children('input').attr('disabled',true);
但没有给出任何结果。
注意:var a
是我从另一个函数获得的元素。
Let's say that I have a DOM object:
var a = document.getElementById('parent')
I want to search all input
inside element a
.
What should I do in jQuery?
I want to disable all input inside a
, like syntax below:
$('#parent input').attr('disabled',true);
I tried
$(a).children('input').attr('disabled',true);
but gave no results.
Note: var a
is an element I got from another function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
children()
仅搜索元素的直接子元素,而find()
搜索所有后代。更新:另请考虑sinsedrix对attr()之间的差异的评论 和
prop()
。children()
just searches immediate children of the element whilefind()
searches all descendants.Update: Also consider sinsedrix's remark on the difference between
attr()
andprop()
.不要忘记
attr
用于 HTML 属性,prop
用于 DOM 属性,试试这个:或者
Don't forget
attr
is for HTML attributes andprop
for DOM properties, try this:or
$(a).find('input').attr('disabled',true)
$(a).find('input').attr('disabled',true)