简体   繁体   English

我正在尝试在<a>标签上设置 class 名称。</a> <a>问题是当我点击链接时页面刷新并且 class 被删除</a>

[英]I'm trying to set a class name on <a> tag. the problem is when I click on the link the page refreshes and the class is removed

I have the following HTML piece of code我有以下 HTML 段代码

<li>
    <a href="javascript:;"><i class="sidebar-item-icon ti-hummer"></i>
        <span class="nav-label">Product Management</span><i class="fa fa-angle-left arrow"></i></a>
    <ul class="nav-2-level collapse">
        <li>
            <a href="/admin/addProducts" id="addProductMenu" onclick="addActiveClass(this)" >Add Product</a>
        </li>
        <li>
            <a href="/admin/allProducts" id="allProductsMenu" onclick="addActiveClass(this)">All Products</a>
        </li>
        <li>
            <a href="/admin/recommendedProducts" id="recommendedMenu" onclick="addActiveClass(this)">Recommended</a>
        </li>
        <li>
            <a href="/admin/featuredProducts" id="featuredMenu" onclick="addActiveClass(this)">Featured</a>
        </li>
        <li>
            <a href="/admin/bestSellingProducts" id="bestSellingMenu" onclick="addActiveClass(this)">Best Selling</a>
        </li>
        <li>
            <a href="/admin/popularIndustriesProducts" id="popularIndustriesMenu" onclick="addActiveClass(this)">Under popular industries</a>
        </li>
    </ul>
</li>

what I want is when I click on any of the <a> link, the javascript function addActiveClass(this) is called and add a class class='active' on the clicked <a> tag.我想要的是当我点击任何一个<a>链接时,调用 javascript function addActiveClass(this)并在点击的<a>标签上添加一个 class class='active'

The javascript code is javascript 代码是

function addActiveClass(element) {
    var el = document.getElementById(element.id);
    el.classList.add("active");
    el.parentElement.classList.add("active");
}

My problem is that when I click on the link the javascript function is executed and the class gets applied.我的问题是,当我单击链接时,将执行 javascript function 并应用 class。 Then after that, the page refreshes and since the class was added dynamically, after re-loading the page, the previously added class class='active' is removed.之后,页面刷新,由于动态添加了 class ,重新加载页面后,之前添加的 class class='active'被删除。

How should I make the class get set after a refresh?刷新后应该如何设置 class?

These lists are on the static page that loads child pages which changes.这些列表位于加载更改的子页面的 static 页面上。

Thanks heleg for your suggestion it actually worked,感谢heleg的建议,它确实有效,

I did the following我做了以下

$(document).ready(function() {

var path = window.location.pathname.split("/").pop();
var parent = null;
var activeElement = null;
var target = null;

if (path == "") {
    path = "home";
}

if (path == "home") {
    target = $('nav div ul li a[href="/admin/' + path + '"]');
} else {
    target = $('nav div ul li ul li a[href="/admin/' + path + '"]');
}

target.addClass("active");

if (path != "home") {
    activeElement = document.getElementsByClassName("active");

    parent = activeElement[0].parentElement;

    parent = parent.parentElement;

    parent.classList.add("in");

    parent = parent.parentElement;

    parent.classList.add("active");
}
});

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

相关问题 当页面刷新 jQuery 时,活动类被删除 - active class is removed when page refreshes jQuery 页面刷新时设置激活 class JQuery? - Set active class when page refreshes JQuery? 单击提交ReactJS时页面刷新 - Page refreshes when I click submit ReactJS 单击注册时页面刷新 - Page refreshes when I click Register 单击时如何将类添加到标签 - how to add class to tag when i click on it 提交名称时,我正在尝试更改我的跨度标签 - I'm trying to change my span tag when name is submitted 我想通过使用样式标签覆盖 css class 属性。 动态更改样式标签值 - I want to override the css class property by using style tag. Change the style tag value dynamically 当我单击取消时,window.print()刷新iframe中的页面 - window.print() refreshes page in iframe when I click cancel 我使用茉莉花来测试click事件,我将删除一个类名,并且我想检测该类名已被删除。 - I use jasmine to test the click event, and I will remove a class name, and I want to detect that the class name has been removed 我在设置innerHTML时遇到问题<tbody>标签。 使用 document.getElementById 选择它后,它向我显示了错误 - I'm facing the problem while setting the innerHTML of the <tbody> tag. After selecting it using the document.getElementById, it shows me the error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM