javascript - 如何制作悬停事件,在jquery中每次都执行而不是第一次,甚至单击内容每次都执行

标签 javascript jquery html

我正在做一个简单的 jQuery 悬停事件,但发生的情况是它仅在第一次执行。我怎样才能让它每次都执行?我的要求是每当我将光标移到 <p> 上时元素或单击其中的内容,它应该以文本形式提醒内容。

<script>
        $(document).ready(function () {
            $("p").hover(function () {
                var s = $(this).text();

                alert(s);
            });
        });

</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
</body>

正如 ritesh 指出的,当我为 webgrid 做的时候,它并不总是给出第一个单元格文本,这是我的代码

网络网格

$(document).ready(function(){
    $("#gridContent tr td:first-child").hover(function(){
      var selectedEmpCode = $(this).text();
                getDepartmentTable(selectedEmpCode);
        },function(){
       var selectedEmpCode = $(this).text();
                getDepartmentTable(selectedEmpCode);
    });
});

我没有得到 $(this).text()

最佳答案

HTML 代码

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").hover(function(){
        $("p").css("background-color", "yellow");
        alert($("p").text());
        },function(){
        $("p").css("background-color", "pink");
    });
});
</script>
</head>
<body>

<p>Hover the mouse pointer over this paragraph.</p>
<p>If you click on me, I will disappear.</p>

</body>
</html>

关于javascript - 如何制作悬停事件,在jquery中每次都执行而不是第一次,甚至单击内容每次都执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060977/

相关文章:

html - 最小宽度和宽度适合内容不适用于文本区域或输入

javascript - 了解 JavaScript 的作用域

javascript - 如何在 Javascript 中推送和拉取多个值

Jquery 无效正则表达式标志 r?

php - 使用 1 个 ajax 发送 2 个请求

html - 这个空白是从哪里来的?

javascript - 使用 D3 (v5) 应用多个过渡

javascript - ON 绑定(bind)过多递归

javascript - jQuery:如何获取外部图像的尺寸?

javascript - jQuery .click() 不适用于使用 AJAX 添加的动态对象