简体   繁体   English

使用Jquery即时更改CSS类

[英]change CSS class on the fly using Jquery

Ok, so I'm a Javascript and jqyery noob, so looking for help. 好的,所以我是Javascript和jqyery noob,所以需要帮助。

I have a CSS menu bar that is used as an element across my site. 我有一个CSS菜单栏,它用作整个网站的元素。 an individual button can be shown as clicked by assigning a class to the element of pressed . 可以示出一个单独的按钮通过向的元件分配一个类作为点击pressed The first link that I have is showing as pressed. 我的第一个链接显示为按下。 I can access the URL easily in my view so I know where I am, but I want to be able to change the class on the fly as the page loads depending on where the url is, so the menu will show selected for that area. 我可以在视图中轻松访问URL,这样我就知道自己在哪里,但是我希望能够根据URL的位置在页面加载时即时更改类,因此菜单将显示为该区域选择的内容。 I've included two elements of my menu below. 我在下面的菜单中添加了两个元素。 I want to change 'class'=> 'pressed' to 'class'=> '' depending on the value in a variable. 我想根据变量中的值将'class'=> 'pressed'更改为'class'=> '' How would I do this? 我该怎么做?

    <ul id="menuBar" class="topmenu">
    <input type="checkbox" id="css3menu-switcher" class="switchbox">
    <label onclick="" class="switch" for="css3menu-switcher"></label>
    <li class="topfirst">
        <?php
        echo $this->Html->link(
            $this->Html->image('icons/group.png', array('width' => '20', 'line-height' => '20'))
            . ' ' . __('Organisations'),
            array('controller' => 'organisations', 'action' => 'index'),
            array('escape' => false, 'class'=> 'pressed'));
        ?>
        <ul>
            <li> <?php
                echo $this->Html->link(
                    $this->Html->image('icons/group_add.png', array('width' => '20', 'line-height' => '20'))
                    . ' ' . __('New Organisation'),
                    array('controller' => 'organisations', 'action' => 'add'),
                    array('escape' => false));
                ?></li>
        </ul>
    </li>
    <li class="topmenu">
        <?php
        echo $this->Html->link(
            $this->Html->image('icons/telephone.png', array('width' => '20', 'line-height' => '20'))
            . ' ' . __('Contacts'),
            array('controller' => 'contacts', 'action' => 'index'),
            array('escape' => false));
        ?>
        <ul>
            <li>  <?php
                echo $this->Html->link(
                    $this->Html->image('icons/telephone_add.png', array('width' => '20', 'line-height' => '20'))
                    . ' ' . __('New contact'),
                    array('controller' => 'contacts', 'action' => 'add'),
                    array('escape' => false, 'class'=> 'unpressed'));
                ?>
        </ul>
    </li>
</ul>

Below are the two expected html outputs for either way: 以下是两种方法的两种预期的html输出:

    <li class="topmenu">
<a href="/dev/oak/trunk/contacts">
<img width="20" alt="" line-height="20" src="/dev/oak/trunk/img/icons/telephone.png">
Contacts
</a></li>


    <li class="topmenu">
<a class='pressed' href="/dev/oak/trunk/contacts">
<img width="20" alt="" line-height="20" src="/dev/oak/trunk/img/icons/telephone.png">
Contacts
</a></li>

I think you can achieve this purely by CSS. 我认为您可以完全通过CSS来实现。

Make all your < a > tags have this class .changeIfActive 使所有<a>标记都具有此类.changeIfActive

Your HTML will look something like 您的HTML看起来像

<a href="your/link/here" class="changeIfActive"> Some text </a>

add to your CSS this class 将此类添加到您的CSS

a.changeIfActive{color:white; background:black;}
a.changeIfActive:active{color:black; background:white;}

Now when user is viewing the target page the CSS will do the CSS class changing using its Psudo-class feature 现在,当用户查看目标页面时,CSS将使用其伪类功能进行CSS类更改

More information please refer to http://www.w3schools.com/css/css_pseudo_classes.asp 有关更多信息,请参阅http://www.w3schools.com/css/css_pseudo_classes.asp

Dz1 Answered Dz1已回答

From what you just said, use "removeClass" to remove the one you want to remove, then "addClass" to add the one that will change the background. 按照您刚才所说的,使用“ removeClass”删除要删除的那个,然后使用“ addClass”添加将改变背景的那个。 -

Try this: 尝试这个:

$("li.topmenu > a").click(function(){
 $("li.topmenu > a").removeClass('pressed');
 $(this).addClass('pressed');
});

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

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