代码之家  ›  专栏  ›  技术社区  ›  Hernán

Firefox中未定义图像ID

  •  1
  • Hernán  · 技术社区  · 15 年前

     $(document).ready(function() {
                $('#img_home').mouseover(function() {
                    img_home.src = 'img/btn/act/home2.gif';
                })
                .mouseout(function() {
                    img_home.src = 'img/btn/pas/home.gif';
                });
    

    在Firefox中,控制台显示未定义的“img_home”。

    <a href="Default.aspx">
        <img alt="home" src="img/btn/pas/home.gif" 
         id="img_home" style="border: none" />
    </a>
    

    有什么想法吗?

    3 回复  |  直到 15 年前
        1
  •  3
  •   Sarfraz    15 年前

    你需要使用 this ,它将引用相关元素:

     $(document).ready(function() {
        $('#img_home').mouseenter(function() {
           this.src = 'img/btn/act/home2.gif';
        })
        .mouseleave(function() {
           this.src = 'img/btn/pas/home.gif';
     });
    
        2
  •  2
  •   lonesomeday    15 年前

    你需要使用 this 要引用元素,而不是其id:

    $(document).ready(function() {
        $('#img_home').mouseover(function() {
            this.src = 'img/btn/act/home2.gif';
        }).mouseout(function() {
           this.src = 'img/btn/pas/home.gif';
        });
    });
    
        3
  •  0
  •   MikeTheReader    15 年前

    你不想:

    $('#img_home').src = ...
    

    推荐文章