0

我正在尝试创建一个给定除法和类列表的函数,然后将在其中进行一些文本替换。

在了解了 Firefox Dom 如何以不同的方式处理文本节点后,我读到我必须使用 javascript 循环遍历元素,兄弟姐妹到 nextSibling。

我在脚本中遇到的最后一个障碍(您可以看到其中的一小部分)是获取类名。我需要类名,以便我可以过滤掉哪些内容被替换的文本。

在查看了所有答案后,在工作中一位名叫 Ryan 的同事的帮助下,我们在 jquery 中重做了这个。

        $(divid).find(".status_bar").each( function() {
        var value = $.trim($(this).text());
        // if value is not defined thru browser bugs do not replace
        if (typeof(value) != 'undefined') {
            // it is a text node. do magic.
            for (var x = en_count; x > 0; x--) {
                // get current english phrase
                var from = en_lang[x];
                // get current other language phrase
                var to = other_lang[x];
                if (value == from) {
                    console.log('Current Value ['+value+'] English ['+from+'] Translation ['+to+']');
                    value = to;
                    $(this).attr('value', to);
                }
            }
        }
    });

这目前适用于所有领域,除了替换文本。

我最初在 jQuery 中执行此操作的原因是,我必须不确定我是否可以循环遍历元素,并避免 firefox 和文本节点的问题。

我正在对 div 中的所有元素进行循环,现在我需要获取我正在循环的元素的类名。

然后我可以检查当前元素的类是否为一,我需要做一些事情......

 // var children = parent.childNodes, child;
    var parentNode = divid;

    // start loop thru child nodes
    for(var node=parentNode.firstChild;node!=null;node=node.nextSibling){

    var myclass = (node.className ? node.className.baseVal : node.getAttribute('class'));

    }

但是这个获取类名的代码只获取空值。

有什么建议么?

对于那些试图弄清楚重点是什么的人,请阅读此JavaScript NextSibling Firefox 错误修复我有代码可以在 Google Chrome 和 IE 中进行我的语言翻译。但是当我在 Firefox 中使用它,并在 ajax 加载它后尝试翻译 div 内容时,由于空格问题,它失败了。

我真的不喜欢 jQuery 或纯 Javascript,我只想要一个可行的解决方案。

谢谢大家的耐心。我个人认为我的描述非常清楚,如果不是,我深表歉意。我并没有试图变得晦涩难懂或难以获得帮助。但请不要侮辱我,暗示我试图让它不清楚。

谢谢。

4

3 回答 3

2

嗯...你有 jQuery 但不使用它?

$(divid).children(".yourSpecialClassName").each( function() {
  doSomethingWith(this);
});

要获取 CSS 类属性值,可以这样做:

$(divid).children().each( function() {
  alert(this.className);
});

根据您现在发布的功能,您需要:

$(divid).find(".status_bar").each( function() {
  $(this).text( function(i, text) {
    var x = $.inArray(en_lang, $.trim(text));
    if (x > -1) {
      console.log('Current Value ['+text+'] English ['+en_lang[x]+'] Translation ['+other_lang[x]+']');
      return other_lang[x];
    }
    return text;
  });
});

请不要再使用“做魔术”作为评论。这是令人难以置信的蹩脚。


编辑。这可以提高效率(多余的console.log()删除):

$(divid).find(".status_bar").each( function() {
  // prepare dictionary en_lang => other_lang
  var dict = {};
  $.each(en_lang, function(x, word) { dict[word] = other_lang[x]; });

  $(this).text( function(i, text) {
    var t = $.trim(text);
    return (t in dict) ? dict[t] : text;
  });
});
于 2010-04-21T15:55:05.660 回答
1

如果您使用的是 jquery,您可以这样做:

$("#myDiv").find("*").each(
   function(){
      var myclass = $(this).attr("class");
   }
);
于 2010-04-21T15:57:47.820 回答
1

您的示例代码没有意义。

$(this).attr('value', to);

'value' 是标签的属性,而不是文本内容。

你真的打算这样做吗?

$(this).text(to);

此外,您已经重新编辑了您的问题,但您仍在尝试使用非 jQuery 方法遍历子节点。你说“我在脚本中遇到的最后一个障碍,你可以看到其中的一小部分,是获取类名。我需要类名,以便我可以过滤掉哪些内容被替换的文本。”

如果您使用的是 jQuery,则完全没有必要遍历任何内容来获取类名。您只需首先使用适当的选择器

 $(divid).find(".status_bar.replaceme").each( function() {  
    // .replaceme is whatever class you're using for the stuff you want to change
    // .status_bar.replaceme matches all elements with BOTH status_bar and replaceme classes

    var value = $.trim($(this).text());
    // if value is not defined thru browser bugs do not replace
    if (typeof(value) != 'undefined') {
        // it is a text node. do magic.


        // NOTE: The following is inefficient but I won't fix it. 
        //       You're better off using an associative array

        for (var x = en_count; x > 0; x--) {
            // get current english phrase
            var from = en_lang[x];
            // get current other language phrase
            var to = other_lang[x];
            if (value == from) {
                console.log('Current Value ['+value+'] English ['+from+'] Translation ['+to+']');
                // value = to;    <-- useless, get rid of it.
                $(this).text(to);
                // or $(this).html(to);
            }
        }
    }
});
于 2010-04-21T17:21:57.357 回答