为什么我必须刷新每个页面才能使jQuery在每个页面上正常工作? [英] Why do I have to refresh each page to get the jQuery to work properly on each of them?

查看:125
本文介绍了为什么我必须刷新每个页面才能使jQuery在每个页面上正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery Mobile,并且试图将一个简单的列表项从一页移动到另一页.这是我正在使用的一小段代码:

I'm using jQuery Mobile and I'm trying to move a simple list item around from one page to another. Here's one snippet of code I'm using:

$('#YesNoMaybePage, #summaryPage').live('pageinit', function() {
$('.green, .blue, .red').click(function(){
    var $move = $('.purchase').prependTo("#summaryPage .theListItem");
});
});

现在这很好用,当我刷新#summaryPage时,单击返回到#YesNoMaybePage并执行事件.但是,如果我刷新#YesNoMaybePage并执行它,它将不起作用.我认为刷新#YesNoMaybePage#summaryPage上的html尚未加载,这就是它不起作用的原因,但老实说我不知道​​.有什么想法可能是为什么以及如何解决这个问题?

Now this works great, when I refresh #summaryPage, click back to #YesNoMaybePage and execute the event. It however does not work if i refresh the #YesNoMaybePage and than execute it. I assume the html on the #summaryPage has not loaded when I refresh the #YesNoMaybePage and than that is the reason why it isn't working, but I honestly don't know. Any ideas why that might be and how I can fix this?

感谢一群世界.我爱你.为了进一步说明,这是我的另一个代码片段,完全相同:

Thanks a bunch world. I love u. For further clarification here's another snippet a code I have with the exact same issue:

$('#YesNoMaybePage, #categorizePage').live('pageinit', function() {
$('.green, .blue, .red').click(function(){
    var $move = $('.purchase').prependTo("#categorizePage .theListItem");
});
});

推荐答案

这两个页面"是否同时在dom中?您似乎在尝试将html元素添加到当前未显示的页面上.那不会实现,您只能修改当前加载的dom.

Are both these 'pages' in the dom at the same time? It appears that you are trying to prepend an html element to a page that isn't currently being displayed. That won't fly, you can only modify the currently loaded dom.

这篇关于为什么我必须刷新每个页面才能使jQuery在每个页面上正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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