如何在jquery中获取当前悬停锚点的值

时间:2010-12-24 10:02:15

标签: jquery

我在这里要做的是获取鼠标悬停的当前链接的值:

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

    $(function(){


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

    });




    });

为了实现这一目标,我需要做些什么?

    </script>
    </head>
    <body>

    <a href='numone'>numone</a>
    <a   href='numtwo'>numtwo</a>

3 个答案:

答案 0 :(得分:3)

如果您只想在鼠标进入元素时获取它:

$(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);
    });

});

(我将其切换为mouseenter,因为您没有使用hover的后半部分,即mouseleave。)

更多jQuery文档:

答案 1 :(得分:1)

试试这个:

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

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

答案 2 :(得分:1)

尝试.text()