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

时间:2015-05-05 18:51:30

标签: 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>
正如我为webgrid所做的那样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()

1 个答案:

答案 0 :(得分:1)

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>