2010-06-18 46 views
3

我使用jQuery的$.live()函数可以点击表格行。
完美适用于Chrome,Firefox甚至桌面Windows Safari - 但不在iPhone上。
$.bind()无处不在,但出于显而易见的原因,我想使用其他功能。

有没有人有任何想法,为什么它不工作,我该如何解决它?
下面的示例代码。与此代码

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<title>test</title> 
<meta charset="utf-8" /> 
<meta name="viewport" content="user-scalable=no,width=device-width" /> 
<meta name="apple-mobile-web-app-capable" content="yes" /> 
<style type="text/css">table { width: 100%; border-collapse: collapse; } table tr {  background: #eee; } table td { padding: 10px; border-top: 1px solid #ccc; }</style> 
<script type="text/javascript" src="http://jquery.com/src/jquery-latest.pack.js">  </script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    /* $.bind() works */ 
    /* 
    $('table').find('tr').bind('click', function() { 
    alert($(this).text()); 
    }); 
    */ 
    /* $.live() doesn't */ 
    $('table').find('tr').live('click', function() { 
    alert($(this).text()); 
    }); 
}); 
</script> 
</head> 
<body> 
<table> 
    <tbody> 
    <tr><td>words are flying out \ </td><td>like endless rain into a paper cup</td></tr> 
    <tr><td>they slither while they pass \ </td><td>they slip away across the    universe</td></tr> 
    </tbody> 
</table> 
</body> 
</html> 

回答

1

的一个问题是,你正在使用.live错误 - 它应该直接选择调用:

$('table tr').live(/* ... */) 

specs

DOM遍历方法并不完全支持查找要发送到.live()的元素。相反,.live()方法应该总是直接选择后调用

接下来,jQuery的1.4.2,它可能会更好使用delegate

$('table').delegate('tr', 'click', function(){/* ... */});