Mootools 获得二级名称

发布于 2024-12-13 02:13:07 字数 512 浏览 6 评论 0原文

我看到使用 jquery 获取第二类名称的方法,但如何在 mootools 中执行此操作,我的元素是

  • 我需要匹配完整的类名称,如 li .parent.active 并调整高度(如果该类存在)我尝试过 $$('li').hasClass('parent active') 或 getProperty 但如果我添加 .parent.active 我明白

    该表达式不是合法表达式。现在我必须支持mootols 1.1、1.2、1.3在CMS上工作,所以我从丑陋的1.1版本开始,

    这就是实际需要的

    var holderdiv =$('mymenu');
    if($$('li.parent.active')){holderdiv.setStyle({'height':'50px'});
    

    I see the method of getting second class name with jquery but how do I do it in mootools , my element is <li class="parent active">

    I need to match full class name like li.parent.active and adjust height if that class is present I tried
    $$('li').hasClass('parent active') or getProperty but all of them return true even if active is not present, if I add .parent.active I get

    The expression is not a legal expression. now I must support mootols 1.1 ,1.2 ,1.3 working on CMS here so I started with the ugly one 1.1 version

    this is how is actually needed

    var holderdiv =$('mymenu');
    if($('li.parent.active')){holderdiv.setStyle({'height':'50px'});
    

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

    发布评论

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

    评论(2

    情绪 2024-12-20 02:13:07

    if($$('li.parent.active') == true) 返回 true,它是 true(已定义,不为 null)。

    如果你的意思是说:是否至少有一个子元素具有 .parent 和 .active,你可以这样做:

    if (holderdiv.getElement("li.parent.active")) // at least one.
        holderDiv.setStyle("height", 50); // setStyle uses a value pair, not an object.
    

    如果它们不是holderdiv的子元素,只需执行 document.getElementwhateverElObj。 getElement 相反。

    由于旧的 xpath 内容,这可能会在 1.11 中中断。

    一种在所有版本中都有效的方法是这样的(尽管只有在 MooTools.version 由于性能成本而错误时才应该这样做)双循环):

    var holderdiv = $("holderdiv");
    
    if (holderdiv.getElements("li.parent").some(function(el){ return el.hasClass("active"); })) {
        // at least one.
        holderdiv.setStyle("height", 50);
    }
    

    .some 将遍历所有 li.parent,直到找到一个与条件匹配的(hasClass("active")) 然后返回布尔值 true否则,错误。

    http://jsfiddle.net/dimitar/BqwAk/

    如果您需要保留 lis 的引用到一个集合中,使用 .filter 代替:

    var lis = holderdiv.getElements("li.parent").filter(function(el){ 
        return el.hasClass("active"); 
    });
    

    如果所有都有 .parent 并且只有一个可以处于活动状态,那么您并不真正关心,因此您可以执行

    if (holderdiv.getElement("li.active"))
    

    if (holderdiv.getElement("li.active").hasClass("parent"))
    

    ...来仔细检查它们是否不存在'都没有.父母。

    if($$('li.parent.active') == true) returns true as in, it's truthy (defined, not null).

    if you mean to say: is there at least one child element that has .parent and .active, you can do:

    if (holderdiv.getElement("li.parent.active")) // at least one.
        holderDiv.setStyle("height", 50); // setStyle uses a value pair, not an object.
    

    if they are not children of holderdiv just do document.getElement or whateverElObj.getElement instead.

    this will likely break in 1.11 due to old xpath stuff.

    one way to do it so it works in all versions would be this (though you should only do this if MooTools.version is wrong due to performance cost of double loops):

    var holderdiv = $("holderdiv");
    
    if (holderdiv.getElements("li.parent").some(function(el){ return el.hasClass("active"); })) {
        // at least one.
        holderdiv.setStyle("height", 50);
    }
    

    the .some will run through all li.parent until it finds one that matches the condition (hasClass("active")) and then return boolean true else, false.

    http://jsfiddle.net/dimitar/BqwAk/

    if you need to keep a reference of the lis into a collection, use .filter instead:

    var lis = holderdiv.getElements("li.parent").filter(function(el){ 
        return el.hasClass("active"); 
    });
    

    if all have .parent and only one can be active, you don't really care so you can just do

    if (holderdiv.getElement("li.active"))
    

    or

    if (holderdiv.getElement("li.active").hasClass("parent"))
    

    ... to double check if they don't all have .parent.

    梦里梦着梦中梦 2024-12-20 02:13:07

    您可以简单地将所有元素与 parentactive 类相匹配并设置它们的样式,MooTools 使这一切变得简单:

    $('li.parent.active').setStyle({'height':'50px'});
    

    You could simply match all elements with both classes parent and active and set their style, MooTools makes that easy:

    $('li.parent.active').setStyle({'height':'50px'});
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文