问题描述:
用iframe做菜单时,二级菜单的src只是后边的参数不同,这时如果只是采用
$('#content iframe').attr('src',href);
这时可以看到iframe的src改变了,但是页面没有跳转,
原因:iframe此时认为你的src没有改变
刷新方法:
document.getElementById(iframe的ID).contentWindow.location.reload(true);不能采用self.location.reload();方法,采用此方法,刷新整个界面,导致iframe回到初始状态代码贴上://点击菜单跳转 var $div = $('.main-sidebar > .sidebar-menu'); $div.on('click', 'li[data-href]', function () { var href = $(this).data('href'); if (!!href && href!='undefined') { var curSelf = $('#content iframe').attr('src'); var curPos = curSelf.lastIndexOf('#'); var curPar = curSelf.substr(curPos); var pos = href.lastIndexOf('#'); var par = href.substr(pos); $('#content iframe').attr('src',href); if(par != curPar && curPos !=-1){ document.getElementById('iframe').contentWindow.location.reload(true); } //菜单增加样式 if($(this).parents('li').data('href') == undefined){ $(this).addClass('active'); $(this).parent('ul').siblings().find('li').removeClass('active'); }else{ $(this).parents('li').addClass('active'); $(this).parents('li').parent('ul').siblings().find('li').removeClass('active'); } } return false; });