jQuery事件处理程序在Chrome popup.js中不起作用

时间:2013-02-10 05:15:42

标签: javascript jquery javascript-events google-chrome-extension event-handling

我有一个popup.html加载jquery然后加载popup.js。

当我将鼠标悬停在具有某个类的div上时,我正在尝试更改光标

popup.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <link type="text/css" rel="stylesheet" href="css/fbh-ui.css" />
    <script type="text/javascript" src="js/jq.js"></script>
    <script type="text/javascript" src="js/popup.js"></script>
</head>

<body style="width: 200px">
    <div id="fbh-main">
        <div class="fbh-popup-menu-item" id="fbh-popup-enabled"></div>
    </div>
</body>

popup.js

$(document).ready(function() {
    $('.fbh-popup-menu-item').mouseenter(function() {
        this.css('cursor', 'pointer');
    });

    $('.fbh-popup-menu-item').mouseleave(function() {
        this.css('cursor', 'default');
    });
});

此代码应该有效。 DOM元素已经存在,因此没有理由不这样做。

1 个答案:

答案 0 :(得分:2)

你写this函数的方式是错误的,它必须像$(this)

以下是更新的代码:

 $(document).ready(function() {
        $('.fbh-popup-menu-item').mouseenter(function() {
            $(this).css('cursor', 'pointer');
        });

        $('.fbh-popup-menu-item').mouseout(function() {
            $(this).css('cursor', 'default');
        });
    });