开发者

How to get value of currently hovered anchor in jquery

What I am trying to do here is to get the value of the current link that is hovered over by the mouse:

    <script type='text/javascript' src='jq.js'></script>
    <script type='text/javascript'>

    $(function(){


    $('a').hover(function(){
    var imgName= $('a[href^=num]').val();
    alert(imgName);

    });




    });

What do I need to do here in order to make that happen?

    </script>
    </head>
    <body>

    <开发者_StackOverflow社区;a href='numone'>numone</a>
    <a   href='numtwo'>numtwo</a>


If you just want to get it when the mouse enters the element:

$(function(){

    $('a').mouseenter(function(){

        // Here, `this` points to the DOM element for
        // the anchor.

        // jQuery helps you get its contents, either as HTML:
        alert($(this).html());
        // Or text:
        alert($(this).text());

        // Or if you just want its href, id, etc., you can
        // just get that directly without a jQuery wrapper:
        alert(this.href);
        alert(this.id);
        alert(this.className);
    });

});

(I switched it to mouseenter since you weren't using the second half of hover, which is mouseleave.)

More in the jQuery docs:

  • mouseenter
  • text
  • html
  • Event handlers


Try .text()


Try this:

<script type='text/javascript' src='jq.js'></script>
<script type='text/javascript'>

    $(
        function()
        {
            $('a').hover
            (
                function()
                {
                    var imgName= $(this).attr("href");
                    alert(imgName);
                }
            );
    }
);
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜