jQuery使用URL在特定页面上展开菜单无cookie
我试图让jQuery菜单在子页面打开时自动展开。具体来说,我需要“服务”部分的子页面,例如“图形设计”、“视频制作”和“web开发”,以在展开状态下显示菜单。有什么建议吗 查看这个JS垃圾箱了解更多信息jQuery使用URL在特定页面上展开菜单无cookie,jquery,Jquery,我试图让jQuery菜单在子页面打开时自动展开。具体来说,我需要“服务”部分的子页面,例如“图形设计”、“视频制作”和“web开发”,以在展开状态下显示菜单。有什么建议吗 查看这个JS垃圾箱了解更多信息 像这样的东西应该可以完成这项工作: // check URL for 'services' (may need to be refined to check a specific location // in the URL structure) if(window.location.href.
像这样的东西应该可以完成这项工作:
// check URL for 'services' (may need to be refined to check a specific location
// in the URL structure)
if(window.location.href.indexOf("services") > -1) {
// expand the UL below the LI containing that menu item
$('a:contains("Services")').parents('li').children('ul').show();
}
注意:这仅适用于初始页面加载或使用“更新”按钮时。必须与检测URL的方式有关。刷新页面,而不是使用“运行”按钮。您可能需要将状态硬编码到不同的页面中,解析URL并从中找出它,或者使用cookie/本地存储,因为没有什么神奇的方法可以做到这一点。你甚至还没有解释你的方法有什么问题。我整个上午都在尝试不同的方法,我在寻找方向……这接近于我之前尝试过的方法,插入这些行的理想位置在哪里?此外,我不断收到上述代码的语法错误。contans后的引号是否需要关闭?如果(window.location.href.indexOf(“services”)>-1{$('a:contains')(“services”)。parents('li')。children('ul')。show();}我修复了语法错误,但是应该在哪里插入您的建议?是的,我缺少一个paren和一个引号。已经修好了。将代码放在您喜欢的任何位置,但将其包装在document.onload中。jQuery速记是$(function(){…});我将其包装在document.onload中,但是.show()似乎没有效果,或者可能是window.location.href.indexOf没有识别出它在“服务”页面上。我以前没有看到.show()的任何用法,但我确实看到了在原始菜单中使用.toggle,这样行吗?我试过了,但运气不好。我可以让我错过一些小东西。你怎么认为?谢谢你的帮助。
jQuery(document).ready(function() {
/*-----------------------------------------------------------------------------------*/
/* Menu
/*-----------------------------------------------------------------------------------*/
if(jQuery('body').hasClass('mobile')){ // Tablet Hover Fix
jQuery("#main-nav li").each(function(){
if ($(this).find(".active").length){
$(this).children('ul').slideToggle('normal');
}
// check URL for 'services' (may need to be refined to check a specific location
// in the URL structure)
if(window.location.href.indexOf("services") > -1) {
// expand the UL below the LI containing that menu item
$('a:contains("Services")').parents('li').children('ul').show();
}
if(jQuery(this).children('ul').length){
var link = jQuery(this).children('a');
if(link.attr('href') == '#'){
link.toggle( function() {jQuery(this).siblings('ul').slideDown("slow"); }, function() {jQuery(this).siblings('ul').slideUp("slow"); });
}
else{
var firstClick = true;
link.click(function(e){
if (firstClick){
jQuery(this).siblings('ul').slideDown("slow");
firstClick = false;
e.preventDefault();
}
});
}
}
});
}
else{
jQuery("#main-nav li").each(function(){
if(jQuery(this).children('ul').length){
jQuery(this).hoverIntent({
interval: 10, /*Original Value 100*/
over: navHoverIn,
timeout: 9900, /*Original Value 300*/
out: navHoverOut
});
}
});
}
function navHoverIn () {
jQuery(this).children('ul').slideDown("slow");
jQuery(this).addClass("down");
}
function navHoverOut () {
var menu = jQuery(this);
jQuery(this).children('ul').slideUp("slow",function() {
menu.removeClass("down");
});
}
var defaultmenu = jQuery('#primary-nav').attr('data-selectname');
jQuery('#main-nav').mobileMenu({
defaultText: defaultmenu
});
jQuery.event.special.debouncedresize.threshold = 250;
// check URL for 'services' (may need to be refined to check a specific location
// in the URL structure)
if(window.location.href.indexOf("services") > -1) {
// expand the UL below the LI containing that menu item
$('a:contains("Services")').parents('li').children('ul').show();
}