【发布时间】:2019-11-24 20:31:57
【问题描述】:
我在 IE 10 及更低版本的导航架中遇到 jquery 代码错误,
导航发生错误,微软页面说使用var insead,但是如何在.find()函数中使用var?
代码如下:
编辑: jquery 代码可与 4 级以上的导航菜单一起使用, 我想我在移动版的菜单中不需要它们,但需要捕捉多个级别。
这里是菜单的完整代码。
$("nav div").click(function() {
$("ul").slideToggle();
$("ul ul").css("display", "none");
$("nav .on").toggleClass("on");
});
$("nav li").click(function(e) {
$(this).find("> ul").slideToggle();
$(this).find("> ul ul").css("display", "none");
$(this).find("> ul li").removeClass("on");
$(this).toggleClass("on");
e.stopPropagation();
});
$(window).resize(function(){
if($(window).width() > 768){
$("ul").removeAttr('style');
}
});
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
background: #e3e3e3;
position: relative;
}
nav li {
display: inline-block;
position: relative;
}
nav a {
color: #292929;
text-decoration: none;
padding: 15px 30px 15px 15px;
display: block;
position: relative;
}
.on > a,
nav li:hover > a {
background: lightgrey;
}
nav ul ul {
position: absolute;
top: 100%;
min-width: 200px;
background: lightgrey;
display: none;
}
nav ul ul ul {
left: 100%;
top: 0;
}
nav ul ul li {
display: block;
background: #e3e3e3;
}
nav ul ul ul li {
background: #eee;
}
/* lets not confuse click with hover for now
nav ul li:hover ul {
display: block;
}
*/
nav li i {
color: #292929;
float: right;
padding-left: 20px;
}
nav div {
background: lightgrey;
color: #292929;
font-size: 24px;
padding: 0.6em;
cursor: pointer;
display: none;
}
nav a:not(:only-child):after {
content: "";
position: absolute;
right: 10px;
top: 50%;
margin-top: -3px;
width: 0;
height: 0;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid #000;
transition: all 0.5s linear;
}
nav li:hover > a:after {
border-top-color: red;
}
.on > a:not(:only-child):after {
transform: rotate(-90deg);
}
ul ul .on > a:not(:only-child):after {
transform: rotate(-90deg);
}
@media (max-width: 768px) {
nav div {
display: block;
}
nav ul {
display: none;
position: static;
background: #e3e3e3;
}
nav ul li {
display: block;
transition: border 0.5s ease;
}
nav ul ul {
position: static;
background: #e3e3e3;
}
ul ul .on > a:not(:only-child):after {
transform: rotate(-180deg);
}
li.on {
background: #fff;
}
ul ul li.on {
border-color: #aaa;
}
ul ul ul li.on {
border-color: #ccc;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
<div> <i class="fa fa-bars">menu</i> </div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">First Level </a>
<ul>
<li><a href="#">Second Level </a>
<ul>
<li><a href="#">Illustration</a></li>
<li><a href="#">Third level </a>
<ul>
<li><a href="#">jQuery</a></li>
<li><a href="#">Vanilla JavaScript</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">jQuery</a></li>
<li><a href="#">Vanilla JavaScript</a></li>
</ul>
</li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
【问题讨论】:
-
您真的需要
find(...)调用中的>吗?我并不是说它是错误的,但我从未见过它以这种方式使用过,到目前为止我还没有找到任何例子。 -
您能否发布相关的 Html 代码(关于导航栏)以重现 Minimal, Complete, and Verifiable example 中的问题。我使用 find 方法创建了一个示例来查找像您这样的元素(例如
$(".nav").find("> span")),它似乎在 IE9+ 中运行良好。所以,这个问题可能和其他代码有关,您最好创建一个示例来重现它,我们可能会更容易帮助您解决问题。 -
好的,我在 jquery 代码中删除了 > 它们,它工作正常,但在 IE/8
addEventListener上出现错误。
标签: jquery ajax windows function internet-explorer