jQuery 递归禁用元素的所有子元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4838209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:01:14  来源:igfitidea点击:

Recursively disabling all children of an element

jquery

提问by lowerkey

This might be a yes/no type of question.

这可能是一个是/否类型的问题。

I'm trying to disable absolutely all children of an element in jquery.

我正在尝试禁用 jquery 中某个元素的所有子元素。

Does calling

是否打电话

$('#id_of_an_element').children().do(function(){
    do_something;
});

recursively call all children of an element, or does it just do_somethingto all the direct descendants of an_element?

递归调用一个元素的所有子元素,还是只调用 的所有do_something直接后代an_element

Help is appreciated,

帮助表示赞赏,

Josh

乔希

回答by karim79

Given a jQuery object that represents a set of DOM elements, the .children() method allows us to search through the immediate children of these elementsin the DOM tree and construct a new jQuery object from the matching elements. The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree. Note also that like most jQuery methods, .children() does not return text nodes; to get all children including text and comment nodes, use .contents().

给定一个表示一组 DOM 元素的 jQuery 对象,.children() 方法允许我们 在 DOM 树中搜索这些元素直接子元素,并从匹配的元素中构造一个新的 jQuery 对象。.find() 和 .children() 方法是相似的,除了后者只沿着 DOM 树向下移动一层。另请注意,与大多数 jQuery 方法一样, .children() 不返回文本节点;要获取所有子节点,包括文本和注释节点,请使用 .contents()。

http://api.jquery.com/children/

http://api.jquery.com/children/

You can do this if you want to act on all descendants at any level of nesting:

如果您想对任何嵌套级别的所有后代进行操作,您可以这样做:

$('#id_of_an_element').find('*').attr('disabled', true);

or using the descendant selector:

或使用后代选择器:

$('#id_of_an_element *').attr('disabled', true);

回答by user113716

Since you want to affect alldescendants, just do this:

既然你想影响所有后代,就这样做:

$('#id_of_an_element *').each(function() {
    // do something
});

But I'd be curious to know what exactly you're doing.

但我很想知道你到底在做什么。

The disabledproperty is meaningless for many element types. It could be that whatever you're doing will benefit from the inheritance of CSS.

disabled属性对于许多元素类型毫无意义。可能你所做的一切都会从 CSS 的继承中受益。

Or if you actually want the disabledproperty, then you might as well just target formelements.

或者,如果您确实想要该disabled属性,那么您也可以只定位form元素。

$('#id_of_an_element :input').attr('disabled','disabled');

回答by Raynos

You can do

你可以做

function disableChildren(obj) {
    obj.children().each(function(i, val) {
         disable(val);
         disableChildren(val);
    });
}

disableChildren($("#id"));

See .eachThere doesn't appear to be a .domethod.

请参阅.each似乎没有.do方法。

You'll have to implement disableas a function to do what every you want when you say "disable"

disable当你说“禁用”时,你必须将其实现为一个函数来做你想做的每一件事