如何在元素中查找元素 [英] How to find an element within an element
本文介绍了如何在元素中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在父li
标记的hover
函数中更改a
标记的字体颜色.
I'm trying to change the font color of a a
tag in the hover
function of the parent li
tag.
我正在尝试做这样的事情
I'm trying to do something like this
$('li').mouseover(function () {
var a = $(this).(NEED TO FIND CORRESPONDING a TAG)
a.css('color', 'white');
});
推荐答案
$(this).find('a');
这将在<li>
元素内找到<a>
元素.
This will find the <a>
element inside the <li>
element.
更好:
$('li a').mouseover(function () {
var a = $(this); // this now refers to <a>
a.css('color', 'white');
});
明智地使用选择器可以避免其他函数调用,从而节省时间.
Using the selector wisely you can save time by avoiding additional function calls.
更好的是,在父<ul>
标记中仅将单个事件侦听器用于鼠标悬停事件.
Even better, use only a single event listener for the mouse over event, in the parent <ul>
tag.
$('ul').mouseover(function (e) {
var a = $(e.target); // e.target is the element which fired the event
a.css('color', 'white');
});
这将节省资源,因为您只使用一个事件侦听器,而不是每个<li>
元素都使用一个.
This will save resources since you only use a single event listener instead of one for each <li>
elements.
这篇关于如何在元素中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文