0

我不确定为什么这不起作用,我在网上阅读的所有文章都使它看起来应该起作用。好吧,这是我的 html 代码:()

<!doctype html>
<html>
<head>
<script src = "http://code.jquery.com/jquery-latest.min.js" type = "text/javascript"></script>
<script src = "showmotto.js" type = "text/javascript"> </script>
</head>

<body>

<table>

<?php

    $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);  
    $sth = $pdo->query('SELECT * from users');
    $sth->setFetchMode(PDO::FETCH_ASSOC);   

    while( $row = $sth->fetch() )
    {
        echo "<tr><td class = \"users\" data-motto = '{$row['motto']}' >{$row['username']}</td></tr>";      
    }

    $pdo = null;

?>

</table>

</body>

</html>

这里是 show-motto.js。我的目标是在用户将鼠标悬停在表格上时使用 data-motto 中的信息提醒用户。但是我还没有走那么远,因为由于某种原因,鼠标悬停事件不起作用

$('td.users').live('mouseover', function()
    alert(0);
});

我也试过 $('.users') 但它也不起作用。

我究竟做错了什么?

4

2 回答 2

4

在最新版本的 jQuery 中,live已被删除。

你可以做的是:

$(document).on('mouseover', 'td.users', function() {
    alert(0);
});

或者,因为元素都是最初创建的:

$(function(){
   // put all your code inside this block
   $('td.users').on('mouseover', function() {
      alert(0);
   });
});

另一种解决方案,类似于第二种解决方案,是将所有 JS 代码放在script位于末尾body(即表格之后)的元素中。

您还添加了一个缺少的左括号。您应该使用显示所有错误的浏览器控制台检测到此问题。

于 2013-04-13T06:19:44.597 回答
1

因为最近版本的 jquery不推荐使用 live 功能......而不是like上使用

$(function(){
    $('td.users').on('mouseover', function()
        alert(0);
    });
})

或者

$(document).ready(function(){
    $('td.users').on('mouseover', function()
        alert(0);
    });
})
于 2013-04-13T06:21:40.603 回答