简体   繁体   English

如何用变量说:如果此ID与此类匹配,则不要这样做

[英]How to say with variables: If this id matches this class don't do this

In words, I want to say, If the body Id is not equal to a list item's class execute the following function, I have tried to get the code right but nothing seems to work. 换句话说,我想说的是,如果正文ID不等于列表项的类,请执行以下功能,我试图使代码正确,但似乎无济于事。

I have 4 pages each with an ID to make the active state work in a css sprite that work great. 我有4个页面,每个页面都有一个ID,以使活动状态在CSS精灵中正常工作。 On top of that I added between normal and hover a jquery opacity effect but the problem is that when I hover the active state It also changes to hover and I want the active sprite to stay put when hover, anyhelp would be appreciated, Saludos. 最重要的是,我在普通和悬停之间添加了一个jQuery不透明效果,但是问题是,当我悬停活动状态时,它也会变为悬停状态,并且我希望活动小精灵在悬停时保持原状,所以不胜感激,Saludos。

Html: HTML:

<ul id="nav">
    <li class="home"><a href="index.html" title="Home Page">Home</a></li>
    <li class="portfolio"><a href="portfolio.html" title="Portfolio Page">Portfolio</a></li>
    <li class="contact"><a href="contact.html" title="Contact Form Page">Contact</a></li>
    <li class="about"><a href="about.html" title="About me Page">About me</a></li>
    </ul>

Jquery: jQuery:

  $(document).ready(function(){

    // Get the ID of the body
    var parentID = $("body").attr("id");

    // Loop through the nav list items
        $("#nav li").each(function() {

        // compare IDs of the body and class of list-items
           var myClass = $(this).attr("class");

        // only perform the change on hover if the IDs don't match (so the active link doesn't change on hover)
        if (myClass != "n-" + parentID) {

   // Opacity effect between states 
    $('ul#nav li a').removeClass('hover');  
    $("ul#nav li a").wrapInner("<span></span>");
    $("ul#nav li a span").css({"opacity" : 0});
    $("ul#nav li a").hover(function(){
        $(this).children("span").stop().animate({"opacity" : 1}, 500);
    }, function(){
        $(this).children("span").stop().animate({"opacity" : 0}, 500);
        });
      }

     });

});

I'm not sure why you're checking for <li> s whose class is not 'n- parentId '. 我不确定为什么要检查类不是'n- parentId '的<li> Don't you just want those whose classes are not the same as the body's ID? 难道你只是想那些类是不一样的身体的ID?

Restrict the <li> selector to only exclude the class you don't want: 限制<li>选择器,使其仅排除不需要的类:

$(document).ready(function(){
    // Get the ID of the body
    var parentId = $("body").attr("id");

    // Loop through the nav list items
    $("#nav li[class!=" + parentId + "]").each(function(){
        // Opacity effect between states 
        $('ul#nav li a').removeClass('hover');  
        $("ul#nav li a").wrapInner("<span></span>");
        $("ul#nav li a span").css({"opacity" : 0});
        $("ul#nav li a").hover(function(){
            $(this).children("span").stop().animate({"opacity" : 1}, 500);
        }, function(){
            $(this).children("span").stop().animate({"opacity" : 0}, 500);
        });
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择所有没有类或ID的元素? - How to select all elements that don't have a class or id? How to say IF班#1在左边,如果班#2在右边在循环 - How do I say IF class #1 on left, if class #2 on right in loop 当错误消息说用户名和密码不匹配时,如何通过Cognito对用户池中的用户进行身份验证? - How do you authenticate a user in a user pool via Cognito when error messages say userName and password don't match even though they do? 如何正确创建Javascript原型,以使类变量在实例化之间不持久? - How to properly create Javascript prototypes such that class variables don't persist between instantiations? 如何将类和 id 设置为 jquery 互不影响的 div? - how to set a class and id to a div that don't affect each other by jquery? 如果您不知道元素的ID或类,如何查找/调试所有jQuery事件绑定的列表 - How to lookup/debug a list of all jQuery events bindings if you don't know the id or class of the elements 变量没有符号的语言如何处理动态调度/调用? - How do languages whose variables don't have sigils deal with dynamic dispatch/invocation? 附加一个具有与类匹配的ID的元素 - Appending an element with an ID that matches a class 隐藏类中与变量 ID 不匹配的所有元素 - Hide all elements within a class that don't match the ID of a variable 为什么变量不更新时数组会更新? - Why do arrays update when variables don't?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM