[英]How to add and remove an HTML class at a specific time after page load?
[英]add class on li remove after page load
我正在使用jquery將類添加到“ li”上的活動CSS上,並且還導航到html page。但是在頁面上導航“ li”上的類分配器之后。 我嘗試了不同的方法來解決此問題,但無法理解。
$(document).ready(function(){
$( '#topList ul li' ).click(function() {
alert($(this).attr('id'));
if($(this).attr('id') == "add") {
document.location.href = 'localhost:8080/add';
$(this).attr('id').addClass("active");
}
});
});
這是菜單列表,我想要的是當我單擊li時它應該調用頁面添加,並在該li上添加一個類。
HTML代碼
<ul class="nav">
<li class="" id="add"><a href="javascript:void(0);" ><i class="add"></i> Add</a> </li>
<ul>
您需要在正在調用的頁面中為li添加類,即,當您調用localhost:8080 / add時將呈現該頁面。 因為在您的代碼中將不會調用活動類的設置,因為localhost:8080 / add將開始在上一行代碼中加載(document.location.href = 'localhost:8080/add';)
如果要呈現的頁面是靜態頁面,則在該頁面中添加此代碼。
$(document).ready(function(){
$('#add').addClass("active");
});
我解決了這個問題, 我的網站上通過查看URL,並決定該導航元素是最好的補充。
function showContent(target) {
var e = $('#'+target);
$(".content").hide();
$("#nav li.active").removeClass("active");
$("#nav li[target='"+target+"']").addClass("active");
e.toggle();
ga('send','pageview',window.location.pathname+"#"+target);
}
// this looks at the URL (by the #...) and decides which view is active
// this gets called on ready and if the client hits the link
// (which dynamically loads instead of making a trip for a new page to the server)
$(window).hashchange(function() {
var which=window.location.hash.substring(1);
switch( which) {
default:
which="profile";
case "profile":
case "resume":
case "projects":
case "contact":
showContent( which);
}
});
在具有菜單或鏈接的頁面上使用以下腳本。
<div id="cssmenu">
<a href="blah blah" > Test page</a>
<a href="blah blah" > Test page</a>
</div>
$(document).ready(function () {
var pageTitle = window.location.pathname.replace(/^.*\/([^/]*)/, "$1");
$('#cssmenu a').each(function () {
if ($(this).attr('href').toLowerCase() == pageTitle.toLocaleLowerCase())
$(this).addClass('active');
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.