我正在使用 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>
此代码的一个问题是您正在使用.live
错误 - 应该直接在选择器上调用:
$('table tr').live( /* ... */)
来自specs http://api.jquery.com/live/:
不完全支持 DOM 遍历方法来查找要发送到 .live() 的元素。相反,.live() 方法应该始终在选择器之后直接调用
接下来,在 jQuery 1.4.2 上,最好使用delegate http://api.jquery.com/delegate/:
$('table').delegate('tr', 'click', function(){/* ... */} );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)