代码之家  ›  专栏  ›  技术社区  ›  mtelis

替换E4X元素中文本的正确方法

  •  2
  • mtelis  · 技术社区  · 14 年前

    var cars = <cars>
                 <buick>
                    <color>
                       "Blue"
                    </color>
                 </buick>
                 <chevy>
                    <color>
                       "Red"
                    </color>
                 </chevy>
               </cars>;
    
    for each (elem in cars)
       for each (item in elem.*)
          elem[item.localName()] = item.text().toString().replace(/"/g,'');
    

    然而,我对这个元素感到不舒服[项目.本地名称()]=施工。事实上,我已经有了一个指向文本项,使用类似以下内容会更符合逻辑:

    item = item.text().toString().replace(/"/g,'');
    

    不幸的是,这个代码似乎没有完成它应该做的。有什么想法为什么?正确的方法是什么?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Zecc    14 年前

    你在哪里运行这个代码?因为它在Firefox中运行得很好。

    for each (txt in cars..*.text()){
        txt.parent().setChildren( txt.toString().replace(/"/g,'') );
    }
    
        2
  •  1
  •   user1158179    13 年前

    请试试这个:

    for each (var color in cars.*.*)
      color.* = color.toString().replace(/"/g,'');