gpt4 book ai didi

dart - 按属性选择元素后代

转载 作者:行者123 更新时间:2023-12-03 03:46:32 26 4
gpt4 key购买 nike

在下面的html中,我想通过属性名称选择 View 标记的后代

<view data-context="testviewmodel">
<div>
id:<input data-bind-two-way="model.id">
name:<input data-bind-two-way="model.name">
description:<input data-bind-two-way="model.description">
</div>

<div>
id:<input data-bind-two-way="model.id">
name:<input data-bind-two-way="model.name">
description<input data-bind-two-way="model.description">
</div>

<div>
<p>{{model.id}}</p>
<p>{{model.name}}</p>
<p>{{model.description}}</p>
</div>
</view>

所以我应该得到6个元素(具有 data-bind-two-way属性的6个输入元素),但是我写了以下递归函数,它给了我3个元素的列表,它们是前三个后代输入元素

 static List<Element> decendantSelector(Element rootElement,{List<Element> collectedElements:null,
List<String> targetAttributes:null,
bool mustHaveAllAttributes:false}){

if(collectedElements==null)
collectedElements = new List<Element>();


for(Element child in rootElement.children){
bool haveAllAttributesFlag = true;
for(String attrName in targetAttributes){
if(child.attributes.containsKey(attrName)){
collectedElements.add(child);
} else {
haveAllAttributesFlag = false;
}
if(!haveAllAttributesFlag && mustHaveAllAttributes)
break;
}
if(child.hasChildNodes())
return decendantSelector(child,
collectedElements:collectedElements,
targetAttributes:targetAttributes,
mustHaveAllAttributes:false);
}
return collectedElements;
}

用作

List<Element> descendantsWithAttributeDataBindTwoWay = decendantSelector(querySelector('view'),targetAttributes:['data-bind-two-way']);

知道为什么第二个div的后代会被忽略吗?

最佳答案

这是因为此return语句

if(child.hasChildNodes())
return decendantSelector(child,

仅删除 return,它将起作用。

你考虑过类似的东西吗

Set<Element> result = new Set<Element>();
result.addAll(querySelectorAll('view [data-bind-two-way]'));
// repeat the same with other combinations until you have covered all cases

关于dart - 按属性选择元素后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24120703/

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