gpt4 book ai didi

JQuery element.children (".class") 返回 null

转载 作者:行者123 更新时间:2023-12-01 08:26:28 25 4
gpt4 key购买 nike

一定有一些明显的事情表明我做错了,但我没有看到它。我已经使用 JQuery 获得了对 DOM 元素的引用。它具有以下innerHTML(来自javascript调试器):

<INPUT type=hidden name=Item.ItemIndicators[0].Indicator.Strategies[0].Description> 
<INPUT style="DISPLAY: none" class=checkbox value=00000000-0000-0000-0000-000000000000 type=checkbox name=Item.ItemIndicators[0].Indicator.Strategies[0].Id>
<INPUT value=-1 type=radio name=Item.ItemIndicators[0].Indicator.Strategies[0].Weight>
<INPUT value=1 type=radio name=Item.ItemIndicators[0].Indicator.Strategies[0].Weight>
<INPUT type=text150 name=Item.ItemIndicators[0].Indicator.Strategies[0].Description>

但是,当我执行以下操作时,我得到 null。

var child = myElement.children(".checkbox");

“checkbox”可能不是一个允许的类名吗?

谢谢!

对于 Matt,完整代码如下:

  var strategies = parent.children("div.strategy");

for (var i = 0; i < strategies.length; i++)
{
//strategies[i] is the element in question w/the innerHTML above
var checkbox = strategies[i].children(".checkbox"); //returns null
...elided...
}

其他信息:如果我执行以下操作,我会得到“对象不支持此属性或方法”:

var strategy = strategies[i];
var children = strategy.children(); //object doesn't support this property or method

不确定此处使用的术语,但从调试器中显示的属性以及此错误来看,jquery 似乎并未将策略对象视为 jquery 对象,而只是将其视为 dom 元素(即,它缺少 [0] 属性)。不过,奇怪的是,“strategies[i].children(".checkbox")”行没有抛出相同的异常。

最佳答案

您需要确保从 jquery 包装集/jquery 对象调用 jquery 方法。

您只需更改代码即可实现此目的,例如

 $(parent).children("div.strategy").each(function(i){
var checkboxes = $(this).children(".checkbox");
...yourstuff ...
//'this' refers to each strategy dom element
}) ;

Children也是 DOM 元素的一个属性,因此可能会引起一些困惑。

关于JQuery element.children (".class") 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3442717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com