javascript - 顶部菜单显示一个空白选项,将我重定向到未定义的页面。我该如何解决?
问题描述
我有一个有一些问题的博客网站。当您尝试在智能手机中打开我的博客或将其放大到 300% 时,您会看到顶部菜单变成了一个下拉菜单,其中第二个位置有一个空白选项。当任何人单击它时,它会重定向到 404 错误页面。请帮我解决这个错误。
我附上一些图片以供参考。
我还将使用它添加菜单条码。
// 插件:SelectNav.js ~ url: https://github.com/lukaszfiszer/selectnav.js window.selectnav=function(){"use strict";var e=function(e,t){function c(e ){var t;if(!e)e=window.event;if(e.target)t=e.target;else if(e.srcElement)t=e.srcElement;if(t.nodeType===3 )t=t.parentNode;if(t.value)window.location.href=t.value}function h(e){var t=e.nodeName.toLowerCase();return t==="ul"|| t==="ol"}函数 p(e){for(var t=1;document.getElementById("selectnav"+t);t++);return e?"selectnav"+t:"selectnav"+(t -1)}函数d(e){a++;var t=e.children.length,n="",l="",c=a-1;if(!t){return}if(c){ while(c--){l+=o}l+=" "}for(var v=0;v"+l+g+"";if(s){var b=e.children[v].children 1;if(b&&h(b)){n+=d(b)}}}}if(a===1&&u){n=''+u+""+n}if(a===1){n= ''+n+""}a--;return n}e=document.getElementById(e);if(!e){return}if(!h(e)){return}if(!("insertAdjacentHTML"in window.document.documentElement)){return}document.documentElement.className+="js";var n=t||{},r=n.activeclass||"active",i=typeof n.autoselect===" boolean"?n.autoselect:true,s=typeof n.nested==="boolean"?n.nested:true,o=n.indent||"-",u=n.label||"Menu", a=0,f="已选择";e.insertAdjacentHTML("afterend",d(e));var l=document.getElementById(p());if(l.addEventListener){l.addEventListener("change" ,c)}if(l.attachEvent){l.attachEvent("onchange",c)}return l};return function(t,n){e(t,n)}}();
解决方案
您可以将此脚本添加到您的网站。我用 chrome 调试工具进行了测试,它工作正常。
当您调整浏览器大小或在 ipad/iphone 中使用时,它将触发代码删除未定义的选项
$(window).resize(function(){
if ($(window).width()< 992){
$('.selectnav option').each(function(index,item){
if($(item).val() == "undefined"){ $(this).remove();}
});
}
});
推荐阅读
- sql-server - 在查询中使用变量时无法查询并将数据放入游标中
- c++ - C++:是否可以操纵整个矩阵而不是单个元素?
- git - 如何在 heroku 上找回丢失的源代码
- javascript - 如何显示具有相同数据ID和类的元素数量?
- azure - powerapps如何退出azure sql连接?
- python - 有没有办法在单击按钮时更改按钮的文本
- java - How to use MVC design pattern in a spring console application
- uml - Visual Paradigm 中代码和模型之间的同步
- python - 如何为 Django 设置本地开发环境
- node.js - How to pass data from node server(google sheet) to angular 9