jQuery:在另一个元素的鼠标悬停时更改某些类

| 好的,我有这段html,它在页面上出现几次,在结构上总是相同的,但是每次链接和链接文本都会与列表文本不同。
<ul>
<li class=\"n1\">Understand key issues relating to creating animations for interactive media products</li>
<li class=\"n2\">Understand key contextual information relating to creating 2D animations for interactive media products</li>
<li class=\"n3\">Be able to create 2D animations for use as part of an interactive media product</li>
<li class=\"n4\">Be able to liaise with relevant parties</li>
<li class=\"n5\">Be able to store 2D animations for use as part of an interactive media product</li>
</ul>
<hr />
<a href=\"alcohol_calculator.php\" class=\"swfselector\">
&raquo; Alcohol unit calculator prototype <img class=\"tab\" src=\"/images/1.png\" width=\"30\" height=\"28\" alt=\"1\" /> <img class=\"tab\" src=\"/images/2.png\" width=\"30\" height=\"28\" alt=\"2\" /><img class=\"tab\" src=\"/images/3.png\" width=\"30\" height=\"28\" alt=\"3\" /> <img class=\"tab\" src=\"/images/5.png\" width=\"30\" height=\"28\" alt=\"5\" />
</a>
<a href=\"bouncing_ball.php\" class=\"swfselector\">
&raquo; Bouncing ball animation <img class=\"tab\" src=\"/images/3.png\" width=\"30\" height=\"28\" alt=\"3\" />
</a>
<a href=\"FOCC_mnemonic.php\" class=\"swfselector\">
&raquo; FOCC mnemonic <img class=\"tab\" src=\"/images/1.png\" width=\"30\" height=\"28\" alt=\"1\" /> <img class=\"tab\" src=\"/images/2.png\" width=\"30\" height=\"28\" alt=\"2\" /> <img class=\"tab\" src=\"/images/3.png\" width=\"30\" height=\"28\" alt=\"3\" /> <img class=\"tab\" src=\"/images/5.png\" width=\"30\" height=\"28\" alt=\"5\" />
</a>
<a href=\"information_literacy_quiz.php\" class=\"swfselector\">
&raquo; Information literacy quiz <img class=\"tab\" src=\"/images/1.png\" width=\"30\" height=\"28\" alt=\"1\" /> <img class=\"tab\" src=\"/images/2.png\" width=\"30\" height=\"28\" alt=\"2\" /> <img class=\"tab\" src=\"/images/3.png\" width=\"30\" height=\"28\" alt=\"3\" /> <img class=\"tab\" src=\"/images/5.png\" width=\"30\" height=\"28\" alt=\"5\" />
</a>
<a href=\"traffic_lights.php\" class=\"swfselector\">
&raquo; Traffic lights <img class=\"tab\" src=\"/images/1.png\" width=\"30\" height=\"28\" alt=\"1\" /> <img class=\"tab\" src=\"/images/2.png\" width=\"30\" height=\"28\" alt=\"2\" /> <img class=\"tab\" src=\"/images/3.png\" width=\"30\" height=\"28\" alt=\"3\" /> <img class=\"tab\" src=\"/images/5.png\" width=\"30\" height=\"28\" alt=\"5\" />
</a>
当鼠标悬停在每个swfselector链接中时,我使用这段Jquery更改图像:
    $(document).ready(function() {
 $(\'.swfselector\').find(\'.tab\').each(function() {
                $(this).attr(\"src\", 
                $(this).attr(\"src\").replace(\".png\", \"o.png\"));  
             })  
});
$(\'.swfselector\').live(\'mouseover mouseout\', function(event) {
  if (event.type == \'mouseover\') {
            $(this).find(\'.tab\').each(function() {
                $(this).attr(\"src\", 
                $(this).attr(\"src\").replace(\"o.png\", \".png\"));  

             })
   } else {
             $(this).find(\'.tab\').each(function() {
                $(this).attr(\"src\", 
                $(this).attr(\"src\").replace(\".png\", \"o.png\"));  
             })
         }
    });
到目前为止,所有这些都有效...但是现在我有了这条Jquery,它试图也更改上面相应元素的类。因此,如果有人将鼠标悬停在包含以下图像的swf选择器上:1.png,3.png和5.png。具有类别:n1,n2和n3的列表元素将更改为:n1o,n3o和n5o。
$(document).ready(function() {
 $(\'.swfselector\').find(\'.tab\').each(function() {
                $(this).attr(\"src\", 
                $(this).attr(\"src\").replace(\".png\", \"o.png\"));  
             })  
});
$(\'.swfselector\').live(\'mouseover mouseout\', function(event) {
  if (event.type == \'mouseover\') {
            $(this).find(\'.tab\').each(function() {
                $(this).attr(\"src\", 
            $(this).attr(\"src\").replace(\"o.png\", \".png\"));  
            var srcString =  $(this).attr(\"src\").substr(0, $(this).attr(\"src\").length - 4);
            nameString =  srcString.substr(8, srcString.length - 1);
            $(this).closest(\'ul\').find(\'li\').each(function() {
            var className = $(this).attr(\'class\');
              if (nameString.indexOf(className)) {
                  $(this).removeClass(className).addClass(className + \'o\');
                  }
                })
             })
   } else {
             $(this).find(\'.tab\').each(function() {
                $(this).attr(\"src\", 
                $(this).attr(\"src\").replace(\".png\", \"o.png\"));  
             })
         }
    });
上面的代码没有返回错误,但没有执行我想要的以太。     
已邀请:
 $(this).closest(\'ul\').find(\'li\').each(function() {
$(this)似乎是指\'。swfselector \',通过使用最接近的值,您正在搜索祖先。但是UL不是\'。swfselector \'的祖先 编辑: 而不是$(this).closest(\'ul \'),您应该这样做:
$(\'ul\').find(\'li\').each(function() {....
甚至更好,为您的\'ul \'提供一个ID
$(\'#myUL\').find(\'li\').each(function() {
    

要回复问题请先登录注册