jQuery $ .live()不适用于iPhone上的表行 [英] jQuery $.live() doesn't work with table rows on the iPhone
问题描述
我正在使用jQuery的 $。live()
功能点击表行。
在Chrome,Firefox甚至桌面上都能完美运行Windows Safari - 但不在iPhone上。
$。bind()
无处不在,但出于显而易见的原因,我想使用其他功能。
I'm making table rows click-able with jQuery's $.live()
function.
Works perfectly on Chrome, Firefox and even desktop Windows Safari -- but not on the iPhone.
$.bind()
works everywhere, but for obvious reasons I'd like to use the other function.
有没有人知道它为什么不起作用以及我该如何解决?
下面的示例代码。
Does anyone have any idea why it doesn't work and how can I fix it?
Example code below.
<!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
错误 - 应该直接在选择器上调用:
One problem with this code is that you're using .live
wrong - it should be called directly on the selector:
$('table tr').live( /* ... */)
来自< a href =http://api.jquery.com/live/ =nofollow noreferrer> specs :
不完全支持DOM遍历方法来查找要发送到.live()的元素。相反,.live()方法应该总是在选择器后直接调用
DOM traversal methods are not fully supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector
接下来,在jQuery 1.4.2上,它可能更好使用 委托
:
Next, on jQuery 1.4.2, it's probably better to use delegate
:
$('table').delegate('tr', 'click', function(){/* ... */} );
这篇关于jQuery $ .live()不适用于iPhone上的表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!