JavaScript 制作标签
所以,我试图制作一个“选项卡菜单”,如下所示: http://flowplayer.org /tools/tabs/index.html (但我不想用这个。) 因此,我尝试使用 url 变量来设置活动菜单项。 代码:
onload=function(){
//Check if editPage is set
if (gup("editPage")) {
gupname = gup("editPage");
var x = "contentEditListItem"+gupname;
var y = document.getElementsByClassName(x);
y.className = "contentEditListItemActive";
}
}
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
gup 函数运行良好,我得到了想要的类名(“contentEditListItem”+gupname)。 为什么它保持不变?
So, I am trying to make a "tabs menu", like this: http://flowplayer.org/tools/tabs/index.html
(but i dont want to use this.)
So, I tried to use url variables to set the active menu item.
Code:
onload=function(){
//Check if editPage is set
if (gup("editPage")) {
gupname = gup("editPage");
var x = "contentEditListItem"+gupname;
var y = document.getElementsByClassName(x);
y.className = "contentEditListItemActive";
}
}
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
The gup function works well, I get the wanted classname ("contentEditListItem"+gupname).
Why it is staying unchanged?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getElementsByClassName
返回一个NodeList
(一种Array
),因此在其上设置属性不会影响该元素的元素它包含,但只包含NodeList
本身,这没有任何作用。您可以设置第一个元素的类名称,例如,如下所示:
或者,如果您希望所有元素都更改其类名称,您可以迭代
NodeList
:getElementsByClassName
returns aNodeList
(kind ofArray
), so setting a property on that is not affecting the elements that it contains, but only theNodeList
itself, which has no effect.You could set the class name of the first element, for example, like this:
Or, if you want all elements have their class name changed, you could iterate over the
NodeList
: