mootools 类名未定义
我真的不明白为什么那段代码不起作用:
$$('.nav_contact').addEvent('click', function(){
if (this.getStyle('color') != '#ffc000') {
this.tween('color','#ffc000');
alert(this.className);
$$('.navigation').getElements('a').each(function(a) {
alert(a.className);
if (a.className != 'nav_contact') {
a.tween('color','#b2b1af');
}
});
}
});
这是相关的 HTML:
<nav class="navigation">
<ul>
<li><a class="nav_foo">Portfolio</a></li>
<li><a class="nav_bar">Services</a></li>
<li><a class="nav_contact">Contact</a></li>
</ul>
</nav>
这应该突出显示单击的按钮并以某种方式隐藏其他按钮。问题是我输入each后无法立即获取元素className。该警报给我“未定义”。 有人吗?
I really don't understand why that piece of code won't work :
$('.nav_contact').addEvent('click', function(){
if (this.getStyle('color') != '#ffc000') {
this.tween('color','#ffc000');
alert(this.className);
$('.navigation').getElements('a').each(function(a) {
alert(a.className);
if (a.className != 'nav_contact') {
a.tween('color','#b2b1af');
}
});
}
});
here is the related HTML :
<nav class="navigation">
<ul>
<li><a class="nav_foo">Portfolio</a></li>
<li><a class="nav_bar">Services</a></li>
<li><a class="nav_contact">Contact</a></li>
</ul>
</nav>
This is supposed to highlight the clicked button and to somehow hide the other ones. The problem is that I can't get elements className soon as I enter the each. The alert gives me "undefined".
Anybody ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将很难按原样缩放/模式化。
考虑这样的事情:
jsfiddle: http://jsfiddle.net/dimitar/V26Fk/
要回答您的问题不过原来的问题。尽管当前 mootools 返回了正确的元素对象,但情况并不总是如此。不要假设它会是这样,并且始终使用 api 来获取对象的属性,例如。
element.get("class")
与el.className
- 它也进行浏览器差异映射。值、文本等也是如此 - 只要约束自己使用 API,否则您将无法升级到 mootools 2.0this will be hard to scale / pattern as is.
consider something like this:
jsfiddle: http://jsfiddle.net/dimitar/V26Fk/
To answer your original question though. Even though CURRENTLY mootools returns a proper element object, it will not always be the case. Do not assume it will be and ALWAYS use the api to get properties of the object, eg.
element.get("class")
vsel.className
- it does the browser differences mapping as well. same for value, text etc - just discipline your self to use the API or you wont be able to upgrade to mootools 2.0