Getelementsbyclassname可以解释一下 [英] Getelementsbyclassname can someone explain

查看:79
本文介绍了Getelementsbyclassname可以解释一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以一步一步解释下面的垫片。 COuld永远不会理解regexp和if blocks下面的块



can some one explain the below shim step by step . COuld never understand the regexp and if blocks below

document.getElementsByClassName= function( className, nodeName ){
    var a = [];
    var re = new RegExp('(^| )'+className+'( |$)');
    var els = nodeName.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; )
       if(re.test(els[i].className))
            a.push(els[i]);
    return a;
}

推荐答案

)');
var els = nodeName.getElementsByTagName( *);
for var i = 0 ,j = els.length; i< j;)
if (re.test(els [i] .className))
a.push(ELS [I]);
返回 a;
}
)'); var els = nodeName.getElementsByTagName("*"); for(var i=0,j=els.length; i<j; ) if(re.test(els[i].className)) a.push(els[i]); return a; }


要了解正则表达式,您需要研究它们并在测试工具中尝试不同的表达式。



这是一个非常好的网站,用于学习正则表达式:

Regular-Expressions。 info - 正则表达式教程,示例和参考 - 正则表达式模式 [ ^ ]



我通常使用的工具是 RegExTest下载| SourceForge.net [ ^ ],它是免费的,漂亮的简单易用。



另一个是 Expresso正则表达式工具 [ ^ ]也是免费的。



代码中使用的正则表达式基本上意味着:将类名称与字符串匹配。



不知道你在哪里得到这段代码,因为已经有了预定义的方法。

HTML DOM getElementsByClassName()方法 [ ^ ]
To understand regular expressions you need to study them and try different expressions in a test tool.

Here is a pretty good site for learning about regular expressions:
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns[^]

The tool I usually use is RegExTest download | SourceForge.net[^] and it is free and pretty simple to use.

Another one is Expresso Regular Expression Tool[^] and is also free.

The regular expression used in your code basically means: Match the class name in a string.

Not sure where you get this code, though, as there is a predefined method already.
See HTML DOM getElementsByClassName() Method[^]


除了解决方案2

Debuggex:在线视觉正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]

是检查RegEx真正做什么的好工具。
In addition to Solution 2
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
is a nice tool to check what a RegEx is really doig.


这篇关于Getelementsbyclassname可以解释一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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