正确的方法来替换E4X元素中的文本 [英] Proper way to replace text in E4X element

查看:153
本文介绍了正确的方法来替换E4X元素中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文本节点中删除双引号。以下代码有效:

I'm trying to remove double quotes from text nodes. The following code works:

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,'');

但是,我觉得不舒服elem [item.localName()] = construction。的确,我已经有一个指向文本的项目,使用类似的东西会更合乎逻辑:

However, I don't feel comfortable about the elem[item.localName()]= construction. Indeed, I already have a pointed to text item and it would be much more logical to use something like:

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

不幸的是,这段代码似乎没有做它应该做的事情。有什么想法吗?这样做的正确方法是什么?

Unfortunately, this code doesn't seem to do what it's supposed to do. Any ideas why? What is the right way to do it?

推荐答案

请试试这个:

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

这篇关于正确的方法来替换E4X元素中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆