今天在处理jquery根据cookie是否存在而使用trigger触发元素的click事件的时候遇到这个问题,各种调试都没能找出引起问题的原因所在,后来在stackoverflow中搜索到相关问题,并用其所提到的原因解决了这个问题。
整理英文原版如下:
question:
I'm figuring out why this simple script is not working:
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery('.next_button a').trigger('click');
});noConflict is necessary because I also load prototype/scriptaculous in this page.
If I replace .trigger('click') with another function (es: .css(...) this works well. Only triggering seems to go broken.
answer:
You can only trigger a click that jQuery has created. It's one of jQuery's cute little quirks.Use the trigger method AFTER you've established the click listener。
我用到这个答案解决了问题,大意是说:你只有在用jQuery创建click方法之后才能去触发click函数,这是jQuery的一个小怪癖。在建立click监听器之后再去使用trigger方法。
而我的代码恰恰就是把click监听器定义在了trigger函数之后,所以页面重新加载的时候无法执行trigger中的click方法,按照这个说法更换了click监听器的位置到trigger触发器之前,就解决了问题。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号