简体   繁体   English

您如何使用jquery将活动类添加到span类链接

[英]How would you add an active class to the span class link using jquery

I copied this script for a pop open box to use for some content on my personal website. 我将此脚本复制到一个弹出框,以用于我的个人网站上的某些内容。 No understanding JS , i was wanting to know if it's possible to add an "active" class to the span class i have labled MANAGE , so i can style it when the content is open. 不了解JS,我想知道是否可以在我标记为MANAGE的span类中添加“活动”类,以便在内容打开时设置样式。 I have tried to use :active in my css but its not working, so i assume its needs added to this script. 我试图在我的CSS中使用:active,但是它不起作用,所以我认为它的需要已添加到此脚本中。

<div id="login_buttons">
<span id="loginlink" class="readmore_jq">MANAGE</span><div class="hide1"><module name="MESSAGE2"/></div>
</div>


<script type="text/javascript">// <![CDATA[
$(document).ready(function(){
    $('.hide1').hide();
    $('.readmore_jq').click(function(){
        var t = $(this);
        t.next().toggle('slow');
    });
});
// ]]></script>

EDITED: i set up in jsfiddle to try all the answers, and still nothing working to get normal and active state http://jsfiddle.net/dEcFL/5/ 编辑:我在jsfiddle中设置尝试所有答案,但仍然无法正常工作并获得正常和活动状态http://jsfiddle.net/dEcFL/5/

you can use toggleClass() to toggle a class on the clicked element : 您可以使用toggleClass()在clicked元素上切换类:

$(document).ready(function(){
    $('.hide1').hide();
    $('.readmore_jq').on('click', function(){
        $(this).toggleClass('active').next().toggle('slow');
    });
});

FIDDLE 小提琴

Since you are using jquery, I think you can try the following code 由于您使用的是jquery,我认为您可以尝试以下代码

$('#loginlink').addClass('active');

besides, there is also a removeClass function. 此外,还有一个removeClass函数。

Hope helps! 希望有帮助!

You can just add a change to the CSS through jQuery after you click it. 您只需单击一下即可通过jQuery向CSS添加更改。 This is an example after the click it will change the text to red works in your js fiddle. 这是单击后的示例,它将在js小提琴中将文本更改为红色。 That should get you on the right path. 那应该使您走上正确的道路。

$(document).ready(function(){
    $('.hide1').hide();
    $('.readmore_jq').click(function(){
        var t = $(this);
        t.next().toggle('slow');
        $( this ).css( "color", "red" );
    });
});
$( "span#loginlink" ).addClass( "active" );

So you want to find whatever span has the word MANAGE and add an active class to it? 因此,您想查找具有单词MANAGE任何span并为其添加active类吗?

Try this jQuery: 试试这个jQuery:

$('span').each(function(){
    if($(this).html() == 'MANAGE')
        $(this).toggleClass('active')
});

All of the below answers are correct: you can add or remove a class using: 以下所有答案都是正确的:您可以使用以下方法添加或删除课程:

$('selector').addClass('class');

or 要么

$('selector').removeClass('class');

or even 甚至

$('selector').toggleClass('class');

See this heavily-upvoted post for more info and please try to search before asking in the future. 有关更多信息,请参见这篇文章该文章获得了很高的评价 ,请在以后询问之前尝试搜索。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM