jQuery $ .live()不适用于iPhone上的表行

编程入门 行业动态 更新时间:2024-10-11 19:15:04
本文介绍了jQuery $ .live()不适用于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="jquery/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( /* ... */)

来自 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上的表行

本文发布于:2023-10-19 18:47:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   jQuery   live   iPhone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!