首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jQuery on()函数和悬停事件

jQuery on()函数和悬停事件
EN

Stack Overflow用户
提问于 2013-07-20 14:25:44
回答 6查看 133关注 0票数 0

我正在使用ajax将XML加载到一个表中,并尝试执行"hover“事件,以便在将鼠标悬停在表行内和表外时更改颜色。表行是使用AJAX动态添加的。这不管用。代码如下:

代码语言:javascript
复制
$(document).ready(function(){
    //$("tr").hover(function(){
    $("#tbl1").on("hover","tr",function(){
       $(this).attr('bgcolor', "yellow");
    },
    function(){
       $(this).attr('bgcolor', "white");
    });     
});

下面是页面加载时的表格

代码语言:javascript
复制
<table width="200" border="5" cellspacing="5" cellpadding="5" id="tbl1">
     <tr>
       <th scope="col">Index</th>
       <th scope="col">Matriks</th>
       <th scope="col">Name</th>
       <th scope="col">IC</th>
       <th scope="col">Age</th>
       <th scope="col">Photo</th>
     </tr>
</table>

提前感谢您的帮助

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-07-20 14:36:27

试试这个:

代码语言:javascript
复制
$(document).ready(function() {
    $("#tbl1").on("mouseenter", "tr", function() {
        $(this).attr('bgcolor', "yellow");
    }).on("mouseleave", "tr", function() {
        $(this).attr('bgcolor', "white");
    });
});
票数 0
EN

Stack Overflow用户

发布于 2013-07-20 14:30:05

使用此函数

代码语言:javascript
复制
$("#tbl1 tr").live("hover",function(){
   $(this).attr('bgcolor', "yellow");
},
function(){
   $(this).attr('bgcolor', "white");
});     
票数 0
EN

Stack Overflow用户

发布于 2013-07-20 14:31:06

尝尝这个

代码语言:javascript
复制
$(document).ready(function(){
    $("#tbl1").on("mouseenter","tr",function(){
       $(this).attr('bgcolor', "yellow");
    },
    function(){
       $(this).attr('bgcolor', "white");
    });     
});

hover是鼠标输入和鼠标离开事件的简写形式。悬停本身并不是一个事件。和.on('hover'..不是有效语法。但是,您可以直接使用$(“#tbl1tr”)函数(.hover() {})。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17759388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档