代码之家  ›  专栏  ›  技术社区  ›  Nicholas Murray

jquery-在type等于image的输入元素中添加onmouseover属性

  •  1
  • Nicholas Murray  · 技术社区  · 15 年前

    是否可以在type等于image的输入元素中添加onmouseover属性?

    我已经创建了以下代码,但它没有添加属性。

        <script language="javascript" type="text/javascript">
            $(document).ready(function() {
                $("input").each(function(i) {
                    if (this.src.indexOf('/images/icons/thumbs.png') != -1) {
                        $(this).attr({ onmouseover: "images/icons/thumbsover.png" });
                        $(this).attr({ onmouseout: "images/icons/thumbsout.png" });
                    }
                });
            });
        </script>   
    
    5 回复  |  直到 15 年前
        1
  •  2
  •   duckyflip    15 年前

    这里有些代码会给你想要的效果。

    $(document).ready(function() {
        $("input:image[src$=thumbs.png]").hover(function() {
            $(this).attr("src", "images/icons/thumbsover.png")
        }, function(){
            $(this).attr("src", "images/icons/thumbsout.png")    
        });
    });        
    

    另外,我会尝试使用纯css实现efect

        2
  •  4
  •   SLaks    15 年前

    你误解了 onmouseover 属性。

    这个 鼠标开关 属性在html中用于提供当鼠标移动到元素上时执行的javascript代码。

    在jquery中,应该使用 event methods 相反。

    你实际上想写以下内容:

    $(":image[src='/images/icons/thumbs.png']").hover(
        function() { this.src = 'images/icons/thumbsover.png' },
        function() { this.src = 'images/icons/thumbsout.png' }
    );
    

    有关详细信息,请阅读 selectors .

        3
  •  1
  •   Sampson    15 年前

    参见jquery的选择器/ Attribute documentation 更多信息。

    $("input[type='image']").hover(
      function () {
        //mouseover
      },
      function () {
        // mouseout
      }
    );
    
        4
  •  1
  •   Gausie    15 年前

    是的,很容易选择:

    $('input[type=image]').mouseover(function(){
        ...
    }).mouseout(function(){
        ...
    });
    

    在这种情况下,似乎要更改背景图像:

    $('input:image[src=/images/icons/thumbs.png]').hover(function(){
        //Mouse Over
        $(this).attr('src','mouseoverimage.gif');
    },
    function(){
        //Mouse Out
        $(this).attr('src','mouseoutimage.gif');
    });
    
        5
  •  0
  •   Marius    15 年前

    当用户悬停图像时,是否尝试更改其源?然后尝试以下操作:

    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $("input").each(function(i) {
                if (this.src.indexOf('/images/icons/thumbs.png') != -1) {
                    $(this).hover(
                      function(){ $(this).attr("src", "images/icons/thumbsover.png");},
                      function(){ $(this).attr("src", "images/icons/thumbsout.png");}
                    );
                }
            });
        });
    </script>