0

好的,我有这段 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>

当鼠标悬停时,我正在使用这段 Jquery 来更改每个 swfselector 链接中的图像:

    $(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"));  
                 })
             }
        });
    

    上面的代码没有返回错误,但没有做我想做的事情。

  • 4

    1 回答 1

    1
     $(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() {
    
    于 2011-04-22T12:40:54.637 回答