【问题标题】:JQuery's hover function does not display the alert messagejQuery的悬停功能不显示警告信息
【发布时间】:2017-04-29 14:56:39
【问题描述】:

我正在尝试显示一个警告框,其中包含“让我们玩得开心!”的消息。当悬停发生时。它必须使用一个包含就绪事件的 jQuery 脚本块和一个 jQuery 语句来侦听主元素中的悬停事件。然后使用 JavaScript 显示警告框。 下面的代码是我所拥有的,但是当我将鼠标悬停到“让我看看”的超链接时,它不会显示警报消息。

我在网上研究过,但不知道是什么错误。

<head>
    <script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
        $('#').hover(function(){
            alert("Lets have fun!");
        });
    });
    </script>
</head>
<main>
<p>Now Now Now <a href="#">Let me see</a></p>
</main>

【问题讨论】:

标签: javascript jquery html hover


【解决方案1】:

在 jQuery 中,“#”代表 id,如 &lt;a href="" id="#"&gt;&lt;/a&gt;

试试这个,$("[href='#']")

$("[href='#']").on('hover', function(){
   alert('Hello');
});

【讨论】:

    【解决方案2】:

    你可以简单地使用一个

    CSS 选择器

    。希望这会有所帮助!

       $("a[href='#']").hover(function () {
            alert("Lets have fun!");
        });
    

    【讨论】:

      【解决方案3】:
      $(document).ready(function() {
        $('* a[href="#"]').on('mouseover', function() { // For all elements <a> that have href="#"
          alert('worked!');
        });
      });
      

      如果你想要“鼠标悬停”的效果

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-19
        • 1970-01-01
        • 2018-10-16
        • 1970-01-01
        • 2011-12-22
        相关资源
        最近更新 更多