dart - 按属性选择元素后代

标签 dart dart-html

在下面的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/

相关文章:

flutter - 使用 'cast' 方法和 'as' 关键字进行转换的区别

dart - 如何比较客户端的系统时间和互联网服务器的日期时间(实际本地时间)

ios - 如何在 flutter 的底部显示切出的字母

dart - 使用ElementList长度和[]时的Dart2js警告

websocket - Dart 中的 Dart websocket :io and dart:html

html - 如何在dart html中创建自定义元素

forms - Dart 未获得输入值

dart - future 的功能不断重复

flutter - 在 flutter navigator 上启动另一个页面时如何调用析构函数?

dart - 检查( native )功能/类/功能(例如 MediaSource)是否可用/受支持