[英]How to not add arrows to links in my submenu in jQuery?
I am writing my own jQuery navigation submenu script. 我正在编写自己的jQuery导航子菜单脚本。 When you hover over a link in the horizontal
nav
that has a ul
tag, it makes that ul
appear. 当您将鼠标悬停在水平
nav
中具有ul
标签的链接上时,它将使该ul
出现。 I have a bit of code that adds an arrow to the links in the horizontal nav
if it has a submenu. 我有一些代码,如果有子菜单,可以向水平
nav
的链接添加箭头。 My problem is that it also adds the arrows to the links in the submenu. 我的问题是,它还将箭头添加到了子菜单中的链接。 This is not a big deal functionally, but it does look bad.
从功能上来说这并不是什么大问题,但是看起来确实很糟糕。
The odd part is that if I use $(this).find('> a')
it screws up the appearance of the submenu. 奇怪的是,如果我使用
$(this).find('> a')
它将$(this).find('> a')
子菜单的外观。 The submenu appears when I hover over the top-level link, but then disappears right away when the mouse leaves that link. 当我将鼠标悬停在顶级链接上时,该子菜单出现,但是当鼠标离开该链接时,该子菜单立即消失。 So I can basically see the entire submenu when the mouse is hovered over the top level link.
因此,当鼠标悬停在顶层链接上时,我基本上可以看到整个子菜单。 When the mouse leaves the top level link, the submenu disappears and I can't click on the submenu links.
当鼠标离开顶层链接时,子菜单消失,并且我无法单击子菜单链接。 What am I doing wrong?
我究竟做错了什么?
Here is a JSFiddle . 这是一个JSFiddle 。 Change
$(this).find('a')
to $(this).find('> a')
and you'll see what I mean. 将
$(this).find('a')
更改$(this).find('> a')
,您将明白我的意思。 Thanks for your time! 谢谢你的时间!
$(document).ready(function(){
$('nav ul li:has(ul)').each(function(){
var listItem = $(this);
$(this).find('> a').each(function(){
var aTag = $(this);
aTag.append('<img src="{img_url}/caret.png" width="8" height="8">');
aTag.on('mouseover', function(){
listItem.find('ul').each(function(){
$(this).css('display', 'block');
});
})
.on('mouseout', function(){
listItem.find('ul').each(function(){
$(this).css('display', 'none');
});
});
});
});
});
you have to pull out the first a
tag from your loop through .each 您必须通过.each从循环中拉出第
a
标签
$(document).ready(function () {
$('nav ul li:has(ul)').each(function () {
var listItem = $(this);
// first a tag as new var
var aTagFirst = listItem.children('a');
aTagFirst.append('<img src="{img_url}/caret.png" width="8" height="8">');
$(this).find('a').each(function () {
var aTag = $(this);
aTag.on('mouseover', function () {
listItem.find('ul').each(function () {
$(this).css('display', 'block');
});
})
.on('mouseout', function () {
listItem.find('ul').each(function () {
$(this).css('display', 'none');
});
});
});
});
});
$(document).ready(function () {
$('nav ul li:has(ul)').each(function () {
var listItem = $(this);
$(this).find('> a,>ul').each(function () {
var aTag = $(this);
aTag.append('<img src="{img_url}/caret.png" width="8" height="8">');
aTag.on('mouseover', function () {
listItem.find('ul').each(function () {
$(this).css('display', 'block');
});
})
.on('mouseout', function () {
listItem.find('ul').each(function () {
$(this).css('display', 'none');
});
});
});
});
});
Here only update is: 这里唯一的更新是:
$(this).find('> a,>ul')
Css: CSS:
only update is one: 唯一的更新是一个:
nav > ul > li > a {
display: block;
margin: 0px;
border-bottom: 0;
color: #333;
height: 52px;
padding: 0px 25px 0px 25px;
font-size: 1.25em;
line-height:55px;
}
Update in padding and add line-height. 更新填充并添加行高。
Demo: http://jsfiddle.net/fsrf5jw3/5/ 演示: http : //jsfiddle.net/fsrf5jw3/5/
You need to add hover callbacks to your li
element, not a
element, so the code becomes: 您需要回调添加悬停您的
li
元素,而不是a
元素,所以代码变成:
var listItem = $(this);
listItem.find('> a').each(function(){
var aTag = $(this);
aTag.append('<img src="{img_url}/caret.png" width="8" height="8">');
});
listItem
.on('mouseover', function(){
listItem.find('ul').each(function(){
$(this).css('display', 'block');
});
})
.on('mouseout', function(){
listItem.find('ul').each(function(){
$(this).css('display', 'none');
});
});
Also, as marsh
answer goes, it is more proper performance-wise to do such things with css, not javascript. 而且,正如
marsh
答案所言,用css(而不是javascript)执行此类操作是更合适的性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.