jQuery仅在首页加载时起作用(ajax异化) [英] Jquery works only on first page load (ajax paganation)

查看:77
本文介绍了jQuery仅在首页加载时起作用(ajax异化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

唯一的问题: jQuery不会触发除第一页以外的其余ajax加载页面的锚链接的click事件.

Only problem: jquery won't trigger the click event of my anchor link for rest of the ajax loaded pages except on first page.

已经找到了与此主题相关的很多答案,但没有一个适合我.

我尝试过的事情: (请看一下.我可能会以错误的方式实现)

1.).在点击事件之前先用div data-role="page"包装:

1.) Wrapping with div data-role="page" first before the click event:

 $('a[id^="preview"]').wrap('<div data-role="page" />');
 //... click event code.. see below

2.).使用body捕获指定链接上的事件

2.) Using the body to capture event on specified link

$('body').on('click','a[id^="preview"]',function(e) {
//... click event code.. see below

3.).尝试将脚本放在头部或身体上.

3.) Tried putting the script on head or in body.

4.).尝试将脚本放置在外部或体内.

4.) Tried putting the the script externally or within body.

我尝试过的所有方法都失败了.只是在第一页上工作.

All of the things I've tried fails. Just working on the 1st page.

这是我的click事件代码,在首次加载(第一页)时效果很好:

Here's my code for the click event and works perfectly on first load (1st page):

$('a[id^="preview"]').on('click',function(e) {   
        alert('preview clicked');
 });

注意:

我正在使用$('a[id^="preview"]'),因为该链接是使用数字后缀动态创建的.例如预览1,预览2 ...等.

I'm using $('a[id^="preview"]') because the link are dynamically created with number suffix. e.g. preview1, preview2...etc.

推荐答案

尝试简化您的示例,并添加此代码以查看是否确实可以从此处扩展.

Try to simplify your example and add this code to see if that works if it does expand fromt here.

演示: http://jsfiddle.net/9hpaL/

$('button').click(function(){
$('body').append('<br><a href="#">Click me<a/>')
});

$('body').on('click', 'a', function(){
alert('It works!');
});

这篇关于jQuery仅在首页加载时起作用(ajax异化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆