2009-07-22 11 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

왜 주위에 span 태그를 넣으려고합니까? 작동하는지 확인하기 위해 다른 방법을 시도해 보셨습니까? 구문이 옳았는지, 그리고 실제로 객체를 선택했는지 확인 했습니까? – Sneakyness

+1

이미지 요소는 IMG 여야하며 이미지 요소에 자식을 추가 할 수 없습니다. –

답변

1

에 무언가를 추가 할 수 없습니다

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

이 작업을 시도 할 수 있습니다 : 당신은이 코드에서 몇 가지 문제가되는 selfclosing 태그

<img src="lol.gif" alt="" /> is a self-closing tag. 
You can only append to not self closing tags like <div></div> 
0

.
먼저 선택이 올바르지 않습니다 : 당신이 사용할 수 있도록
잘못된

$('image') 

올바른

$('img') 

둘째, img 요소는 할 수 없습니다 "찾기"내가 아는 한 자식 요소를 포함 명령.

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" /> 

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

    }); 
api.jquery.com을 읽어주세요, 그것의 다소 빠른 라이브러리 가지 작은대로를 통해 읽어 보시기 바랍니다.

관련 문제