简体   繁体   English

如何将CSS动态添加到菜单项

[英]How to add CSS dynamically to the Menu Items

I want to Highlight the Menu Dynamically in Html Pages using Js. 我想使用Js在HTML页面中动态突出显示菜单。

For Example 例如

<div id="cssmenu">
<ul id="myid">
    <li id="m1"><a href="company.html">COMPANY</a></li>
    <li id="m2" class="has-sub"><a href="#">SERVICES</a>
        <ul>
            <li class="has-sub"><a href="#">Enterprise Solution</a>
                <ul>
                    <li><a href="sap.html">SAP</a></li>
                    <li><a href="oracle.html">Oracle</a></li>
                </ul>
    </li>
</div>

I given Like this. 我给这样。 But its not working 但是它不起作用

<script>

$(document).ready(function() {
    $("#cssmenu ul li").click(function() {

        $(this).addClass('active').siblings('li').removeClass('active');

    });
});

For this i would like activate the Menu when it is Clicked Using Js. 为此,我想在使用Js单击菜单时激活菜单。 Please Help me. 请帮我。

Thanks in Advance. 提前致谢。

I have a demo for you here: http://jsfiddle.net/ttaN2/4/ 我在这里为您提供了一个演示: http : //jsfiddle.net/ttaN2/4/

I have altered the HTML so that there are correct open and close tags: 我已经更改了HTML,以便有正确的打开和关闭标签:

<div id="cssmenu">
    <ul id="myid">
        <li id="m1"><a href="company.html">COMPANY</a></li>
        <li id="m2" class="has-sub"><a href="#">SERVICES</a></li>
        <li class="has-sub"><a href="#">Enterprise Solution</a>
            <ul>
                <li><a href="sap.html">SAP</a></li>
                <li><a href="oracle.html">Oracle</a></li>
            </ul>
        </li>
    </ul>
</div>

I believe this is what you intended. 我相信这就是您的意图。 I am not sure what you intended to happen when you click on another sub menu though. 我不确定您单击另一个子菜单时的意图。 I'll try to help you out if you can describe exactly what you intend to happen. 如果您能准确描述您打算做什么,我会尽力帮助您。

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

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