mootools 类名未定义

发布于 2024-12-06 15:12:56 字数 916 浏览 0 评论 0原文

我真的不明白为什么那段代码不起作用:

$$('.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ懵逼小萝莉 2024-12-13 15:12:56

这将很难按原样缩放/模式化。

考虑这样的事情:

(function() {
    var navItems = $(".navigation a");

    document.getElements("a.nav_contact").addEvent("click", function() {
        var self = this;
        if (this.getStyle('color') != '#ffc000') {
            this.tween('color', '#ffc000');

            navItems.each(function(a) {
                // more scalable - change all but the current one w/o special references.
                if (a != self) {                      
                    a.tween('color', '#b2b1af');
                }

                return;
                // or check if it has an implicit class name...
                if (!a.hasClass("nav_contact"))
                    a.tween('color', '#b2b1af');

                // or check if the only class name is matching...
                if (a.get("class") != 'nav_contact')
                    a.tween('color', '#b2b1af');


            });
        }
    });
})();

jsfiddle: http://jsfiddle.net/dimitar/V26Fk/

要回答您的问题不过原来的问题。尽管当前 mootools 返回了正确的元素对象,但情况并不总是如此。不要假设它会是这样,并且始终使用 api 来获取对象的属性,例如。 element.get("class")el.className - 它也进行浏览器差异映射。值、文本等也是如此 - 只要约束自己使用 API,否则您将无法升级到 mootools 2.0

this will be hard to scale / pattern as is.

consider something like this:

(function() {
    var navItems = $(".navigation a");

    document.getElements("a.nav_contact").addEvent("click", function() {
        var self = this;
        if (this.getStyle('color') != '#ffc000') {
            this.tween('color', '#ffc000');

            navItems.each(function(a) {
                // more scalable - change all but the current one w/o special references.
                if (a != self) {                      
                    a.tween('color', '#b2b1af');
                }

                return;
                // or check if it has an implicit class name...
                if (!a.hasClass("nav_contact"))
                    a.tween('color', '#b2b1af');

                // or check if the only class name is matching...
                if (a.get("class") != 'nav_contact')
                    a.tween('color', '#b2b1af');


            });
        }
    });
})();

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") vs el.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文