2009-07-22 38 views
0

我尝试下面的代码:悬停不适用于图像?

<script type="text/javascript">  
$(document).ready(function(){ 
    $("image").hover(
     function() { 
     $(this).append($("<span> ***</span>")); 
     }, 
     function() { 
     $(this).find("span:last").remove(); 
     } 
    ); 

    }); 

<img alt="" src="../../Content/Disp.gif" /> 

它没有工作。甚至使用$(“img”)。这是否意味着悬停不起作用的形象?

+0

你为什么想把标签放在它周围?你有没有尝试过,看看它是否有效?你确定你的语法是正确的,而且你确实选择了对象? – Sneakyness 2009-07-22 17:05:10

回答

1

有这个代码的一些问题。
第一选择是不正确的:
错误

$('image') 

正确

$('img') 

其次,img元素不能包含子元素,据我知道,所以你不能使用“find”命令与它。

$(document).ready(function(){ 
    $("img").hover(
     function() { 
     $(this).append($("<span>***</span>")); 
     }, 
     function() { 
     $(this).find("span:last").remove(); // this not correct 
     } 
    ); 

    }); 
2

你可以试试这个:

$("img").each(function(){ 
    $(this).hover(function(){ 
     $(this).after("<span>Foobar</span>"); 
    }, function(){ 
     $(this).find("span:last").remove(); 
    }); 
}); 
0

你不能将一些东西附加到自闭合标签

<img src="lol.gif" alt="" /> is a self-closing tag. 
You can only append to not self closing tags like <div></div> 
0
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $('img').hover(function(){ 
      $('span').html('Mouse over image'); 
     }); 
     $('img').mouseout(function(){ 
      $('span').html('Mouse not over image'); 
     }); 
    }); 
</script> 

<img alt="" src="http://stackoverflow.com/content/img/so/logo.png" /> 
<span>Mouse not over image</span> 
0
<script type="text/javascript">  
$(document).ready(function(){ 
    $("img").hover(
     function() { 
     $(this).after("<span> ***</span>"); 
     }, 
     function() { 
     $(this).next().remove(); 
     } 
    ); 
    }); 
</script> 

<img alt="" src="../../Content/Disp.gif" /> 

请务必阅读api.jquery.com,这是一个相当快读完的图书馆是一种微小的。