jQuery 切换下划线

发布于 2024-10-11 10:55:24 字数 1256 浏览 3 评论 0原文

我有一个通过单击链接进行切换的菜单。我希望该链接在菜单可见时带有下划线,并且在隐藏时没有任何装饰。我无法正确使用该功能:当您第一次按下触发按钮时会显示下划线,但在隐藏菜单后仍会保留下划线。

感谢您的帮助!

HTML

<div id="trigger">
        <div id="menu">MENU<br/></div>
        <div id="navdrop">
            <a class="a transition" id="item1" href="Pages/music.html" alt="MUSIC">MUSIC</a><br />
            <a class="a transition" id="item2" href="Pages/photos.html" alt="PHOTOS">PHOTOS</a><br />
            <a class="a transition" id="item3" href="Pages/biography.html" alt="BIOGRAPHY">BIOGRAPHY</a><br />
            <a class="a transition" id="item4" href="Pages/discography.html" alt="DISCOGRAPHY">DISCOGRAPHY</a><br />
            <a class="a transition" id="item5" href="Pages/contact.html" alt="CONTACT">CONTACT</a><br />
            <a class="a" id="item6" href="http://www.blog.fernandogaribay.com" alt="BLOG" target="_blank">BLOG</a><br />
        </div>
    </div>

JavaScript

    $('#trigger').click(function (){
        $('#navdrop').slideToggle(400);
        $('#menu').css("text-decoration", "underline");
});

I've got a menu that toggles by clicking a link. I'd like that link to underline when the menu is visibile, and have no decoration when it's hidden. I can't get the function correct: the underline displays when you first press the trigger button, but stays after the menu is hidden.

Thanks for your help!

HTML

<div id="trigger">
        <div id="menu">MENU<br/></div>
        <div id="navdrop">
            <a class="a transition" id="item1" href="Pages/music.html" alt="MUSIC">MUSIC</a><br />
            <a class="a transition" id="item2" href="Pages/photos.html" alt="PHOTOS">PHOTOS</a><br />
            <a class="a transition" id="item3" href="Pages/biography.html" alt="BIOGRAPHY">BIOGRAPHY</a><br />
            <a class="a transition" id="item4" href="Pages/discography.html" alt="DISCOGRAPHY">DISCOGRAPHY</a><br />
            <a class="a transition" id="item5" href="Pages/contact.html" alt="CONTACT">CONTACT</a><br />
            <a class="a" id="item6" href="http://www.blog.fernandogaribay.com" alt="BLOG" target="_blank">BLOG</a><br />
        </div>
    </div>

Javascript

    $('#trigger').click(function (){
        $('#navdrop').slideToggle(400);
        $('#menu').css("text-decoration", "underline");
});

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

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

发布评论

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

评论(2

剩余の解释 2024-10-18 10:55:24
$('#trigger').click(function (){
    $('#navdrop').slideToggle(400, function(){
         $('#menu').toggleClass('underline');
    });
});

css

#menu.underline {
   text-decoration: underline;
}

移动演示

$('#trigger').click(function (){
    $('#navdrop').slideToggle(400, function(){
         $('#menu').toggleClass('underline');
    });
});

css

#menu.underline {
   text-decoration: underline;
}

demo on the go

无人问我粥可暖 2024-10-18 10:55:24

您需要使用 .slideToggle() 的回调部分,

$('#trigger').click(function (){
    $('#navdrop').slideToggle(400, function() {
        $('#menu').toggleClass('underline');
    });

});

这将在幻灯片完成后触发 css 更改。

我还建议您在 css 中设置一个 .underline 类来设置下划线。正如其他人一样,我也会为您提供 css 规则,非常基本。使用通用 .underline 还可以提高可重用性,因为您可以在站点中的任何位置使用它来设置下划线文本装饰

.underline {
   text-decoration: underline;
}

you need to use the callback portion of the .slideToggle()

$('#trigger').click(function (){
    $('#navdrop').slideToggle(400, function() {
        $('#menu').toggleClass('underline');
    });

});

This will trigger the css change after the slide has completed.

I also suggest you set up a .underline class in your css to set the underline. As others have I will also give you the css rule for this, very basic. Using a generic .underline also improves reusability as you can use it anywhere in your site to set the underline text-decoration

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