如何在元素中查找元素

我正在尝试更改父li标签的hoverfunction中标签的字体颜色。

我正在尝试做这样的事情

 $('li').mouseover(function () { var a = $(this).(NEED TO FIND CORRESPONDING a TAG) a.css('color', 'white'); }); 

 $(this).find('a'); 

这将在

  • 元素中找到元素。

    更好的是:

     $('li a').mouseover(function () { var a = $(this); // this now refers to  a.css('color', 'white'); }); 

    明智地使用选择器可以避免额外的函数调用,从而节省时间。

    更好的是,在父

      标记中仅使用单个事件侦听器来处理事件。

       $('ul').mouseover(function (e) { var a = $(e.target); // e.target is the element which fired the event a.css('color', 'white'); }); 

      这将节省资源,因为您只为每个

    • 元素使用单个事件侦听器而不是一个。

      尝试:

       var a = $(this).find("a"); 
       $('li').mouseover(function () { var a = $("a", this); a.css('color', 'white'); }); 

      第二个参数指定上下文。 所以它在这个上下文中找到了所有的li

      您可以使用jQuery .find()来查找元素

      如果你的父标签是

    • 并且您的子标记是

      然后你可以使用这个找到子标签:

       $('li').mouseover(function () { var innerElement=$(this).find("div"); innerElement.css('color', 'white'); }