如何解决iPhone上动态HTML元素点击事件失效问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计200个文字,预计阅读时间需要1分钟。
在iPhone上,对于动态生成的HTML元素绑定点击事件无效的问题,可以尝试以下解决方案:
1. 确保事件绑定在DOM元素完全加载后进行。可以使用`$(document).ready()`来确保文档已加载完成。
2. 使用`$(document).on()`方法来绑定事件,这样可以绑定到未来可能添加到DOM中的元素。
以下是修改后的代码示例:
javascript$(document).ready(function() { $(document).on('click', 'selector', function() { // 事件处理函数 });});
Iphone上对于动态生成的html元素绑定点击事件$(document).click()失效解决办法stackoverflow.com/questions/3705937/document-click-not-working-correctly-on-iphone-jquery $(document).on('click','selector',function(){}); // 或者使用touchstart或者touchend代替click事件。触屏事件问题,如果触发了touchmove,touchend就不会被触发了,而且touchmove没有持续触发。 // 在touchstart的时候调用下event.preventDefault(); 即可让其他事件都正常被触发了。 // 只有链接a和input输入框这两类元素才有click事件。 // 在元素上加css属性cursor:pointer;
本文共计200个文字,预计阅读时间需要1分钟。
在iPhone上,对于动态生成的HTML元素绑定点击事件无效的问题,可以尝试以下解决方案:
1. 确保事件绑定在DOM元素完全加载后进行。可以使用`$(document).ready()`来确保文档已加载完成。
2. 使用`$(document).on()`方法来绑定事件,这样可以绑定到未来可能添加到DOM中的元素。
以下是修改后的代码示例:
javascript$(document).ready(function() { $(document).on('click', 'selector', function() { // 事件处理函数 });});
Iphone上对于动态生成的html元素绑定点击事件$(document).click()失效解决办法stackoverflow.com/questions/3705937/document-click-not-working-correctly-on-iphone-jquery $(document).on('click','selector',function(){}); // 或者使用touchstart或者touchend代替click事件。触屏事件问题,如果触发了touchmove,touchend就不会被触发了,而且touchmove没有持续触发。 // 在touchstart的时候调用下event.preventDefault(); 即可让其他事件都正常被触发了。 // 只有链接a和input输入框这两类元素才有click事件。 // 在元素上加css属性cursor:pointer;

