Javascript 如何获取元素';元素循环中的类名是什么?
我试图创建一个函数,给定一个divid和一个类列表,然后在其中进行一些文本替换 在了解了FirefoxDOM如何以不同的方式处理文本节点后,我了解到我必须使用javascript循环元素,与nextSibling同级 我在脚本中遇到的最后一个障碍是获取类名,您可以看到其中的一小部分。我需要类名,这样我就可以过滤掉被替换的内容 在查看了所有答案之后,在同事Ryan的帮助下,我们在jquery中重做了这一点Javascript 如何获取元素';元素循环中的类名是什么?,javascript,jquery,element,classname,Javascript,Jquery,Element,Classname,我试图创建一个函数,给定一个divid和一个类列表,然后在其中进行一些文本替换 在了解了FirefoxDOM如何以不同的方式处理文本节点后,我了解到我必须使用javascript循环元素,与nextSibling同级 我在脚本中遇到的最后一个障碍是获取类名,您可以看到其中的一小部分。我需要类名,这样我就可以过滤掉被替换的内容 在查看了所有答案之后,在同事Ryan的帮助下,我们在jquery中重做了这一点 $(divid).find(".status_bar").each( fun
$(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中的所有元素进行循环,现在需要获取循环所依据的元素的类名
然后我可以检查当前元素的类是否为1,我需要对
// 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'));
}
但是,这段用于获取类名的代码只包含get的null值
有什么建议吗
对于那些想弄清楚要点的人,请阅读本文,我有一些代码可以在Google Chrome和IE中进行语言翻译。但是当我在Firefox中使用它,并尝试在ajax加载div内容后翻译它时,由于空格问题,它失败了
我真的不喜欢jQuery或纯Javascript,我只想要一个工作的解决方案
谢谢大家的耐心。我个人认为我的描述非常清楚,如果不是,我道歉。我并不是故意装模作样,也不是故意让人难以得到帮助。但请不要侮辱我,暗示我想把事情弄清楚
谢谢。嗯。。。您有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()
已删除):
嗯。。。您有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()
已删除):
如果使用jquery,可以执行以下操作:
$("#myDiv").find("*").each(
function(){
var myclass = $(this).attr("class");
}
);
如果使用jquery,可以执行以下操作:
$("#myDiv").find("*").each(
function(){
var myclass = $(this).attr("class");
}
);
您的示例代码没有意义
$(this).attr('value', to);
“value”是标记的属性,而不是文本内容
你真的想这么做吗
$(this).text(to);
此外,您已经编辑了问题,但仍在尝试使用非jQuery方法循环子节点。你说“我在脚本中遇到的最后一个障碍,你可以看到其中的一小部分,就是获取类名。我需要类名,这样我就可以过滤掉get文本替换的内容。”
如果您使用的是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);
}
}
}
});
$(divid).find(“.status\u bar.replaceme”).each(function(){
//.replaceme是您用于更改内容的任何类
//.status\u bar.replaceme将所有元素与status\u bar和replaceme类匹配
var值=$.trim($(this.text());
//如果未通过浏览器定义值,则不替换错误
如果(类型(值)!=‘未定义’){
//这是一个文本节点。请施展魔法。
//注意:以下是低效的,但我不会修复它。
//最好使用关联数组
对于(var x=en_count;x>0;x--){
//获取当前英语短语
var from=en_lang[x];
//获取当前其他语言短语
var to=其他_lang[x];
如果(值==从){
log('Current Value['+Value+']English['+from+']Translation['+to+'];
//value=to;您的示例代码没有意义
$(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);
}
}
}
});
$(divid).find(“.status\u bar.replaceme”).each(function(){
//.replaceme是您用于更改内容的任何类
//.status\u bar.replaceme将所有元素与status\u bar和replaceme类匹配
var值=$.trim($(this.text());
//如果未通过浏览器定义值,则不替换错误
如果(类型(值)!=‘未定义’){
//这是一个文本节点。请施展魔法。
//注意:以下是低效的,但我不会修复它。
//最好使用关联数组
对于(var x=en_count;x>0;x--){
//获取当前英语短语
var from=en_lang[x];
//获取当前其他语言短语
var to=其他_lang[x];
如果(值==从){
log('Current Value['+Value+']English['+from+']Translation['+to+'];
//value=to;如何获得循环中节点的类名?我必须循环通过div中的所有元素。但是我不能循环数字,因为t