jQuery悬停和click方法不适用于同一元素

时间:2018-10-02 05:03:34

标签: jquery

jQuery的一种方法是为同一元素的单击和鼠标悬停事件安装事件处理程序。问题在于,当我只使用悬停方法时,它可以工作,但不能与单击方法一起工作。

<!DOCTYPE html> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyInformation</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
    <p id = "p1">This is paragraph 1</p>
    <p id = "p2">This is paragraph 2</p>
    <p id = "p3">This is paragraph 3</p>
    <script>
        $(p1).on
        ({
            mouseenter: function () 
            {
                $(this).css("color","red");
            },
            mouseleave: function () {
                $(this).css("color","black");
            }
            click: function () {
                alert("hello");
            }
         });
    </script>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

您的选择器错误,语法有些错误,这是固定版本:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyInformation</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
    <p id = "p1">This is paragraph 1</p>
    <p id = "p2">This is paragraph 2</p>
    <p id = "p3">This is paragraph 3</p>
    <script>
        $('#p1').on
        ({
            mouseenter: function () {
                $(this).css("color","red");
            },
            mouseleave: function () {
                $(this).css("color","black");
            },
            click: function () {
                alert("hello");
            }
         });
    </script>
</body>
</html>

答案 1 :(得分:0)

df1 = df.groupby([pd.Grouper(freq='1D'), 'A']).size().unstack(fill_value=0) #alternative #df1 = df.groupby(pd.Grouper(freq='1D'))['A'].value_counts().unstack(fill_value=0) df1['new'] = df1['Passed'].div(df1.sum(axis=1)) #alternative #df1['new'] = df1['Passed'].div(df1['Passed'].add(df1['Failed'])) print (df1) A Failed Passed new 2018-01-01 5 3 0.375 2018-01-02 5 3 0.375 2018-01-03 4 4 0.500 2018-01-04 4 4 0.500 2018-01-05 4 4 0.500 2018-01-06 3 5 0.625 2018-01-07 4 4 0.500 2018-01-08 5 3 0.375 2018-01-09 4 4 0.500 2018-01-10 4 4 0.500 2018-01-11 4 4 0.500 2018-01-12 7 1 0.125 2018-01-13 0 4 1.000 函数之后您错过了逗号

mouseleave

答案 2 :(得分:0)

您的代码中有两个错误

  1. $(p1)应该是$('#p1')
  2. mouseleave 功能后缺少逗号

    mouseleave: function () {
        $(this).css("color","black");
    },
    click: function () {
        alert("hello");
    }