jQuery根本不起作用

jQuery simply not working?

本文关键字:不起作用 jQuery      更新时间:2023-09-26

>我有一个包含以下div的网页:

<div class='book_box'>
    <table border=0 cellspacing=0>
        <tr>
            <td align='center'><img src='img/theseven.jpg' /></td>
        </tr>
        <tr>
            <td align='center'>The Seven by Derek Edgington</td>
        </tr>
    </table>
</div>

我的标头中有以下Javascript代码:

$('.book_box').hover(
    function() {
        $(this).css('background', '#ffffff');
    },
    function() {
        $(this).css('background', '#a8ff9a');
    }
);

。它根本不起作用。我已经测试并确保 jQuery 正确加载,并用它执行了一些其他操作。这段代码似乎不起作用?

在代码没有任何变化的情况下,它在这里为我工作是JSFiddle上的演示

演示

建议:只需利用$(document).ready

$(document).ready(function() {
  $('.book_box').hover( function() {
      alert('abc');
      $(this).css('background', '#ffffff'); 
    },
    function() { 
      alert('abc1');
      $(this).css('background', '#a8ff9a'); 
    } 
  );  
});

你需要把代码放在 dom ready 回调函数中。或者,您的代码将在div 准备就绪之前执行.book_box

$(function() {
  // put your code here.
});

这是捷径

$(document).ready(function() {
  // put your code here.
});

尝试:

 $('.book_box').hover(
function() {
    $(this).css(backgroundColor, '#ffffff');
},
function() {
    $(this).css(backgroundColor, '#a8ff9a');
}
);

在 Linux 上的 Firefox 和 Opera 上,它工作正常,但也许你试试这个:

$(function() {
            $('.book_box table').hover(
            function() {
                $(this).css('background', '#ffffff');
            },
            function() {
                $(this).css('background', '#a8ff9a');
            }
        );
});

你看过(火狐)错误控制台吗?