简体   繁体   English

jQuery&jqTouch-ajax.load问题

[英]jQuery & jqTouch - ajax.load problem

I think my problem lies more in my approach and applies more to jquery, but something weird is happening here. 我认为我的问题更多地在于我的方法,并且更多地适用于jquery,但是这里发生了一些奇怪的事情。

I'm using jQuery & jqTouch. 我正在使用jQuery&jqTouch。 My html is: 我的html是:

<div id="home" class="current">
    <div class="toolbar">
        <h1>Header</h1>
    </div>
    <ul class="rounded">
        <li class="arrow"><a href="#currentloc">Current Location</a></li>
    </ul>       
</div>
<div id="currentloc">
    <div class="toolbar">
        <h1>Nearby</h1>
    </div>
</div>

What I'm trying to do is when a user clicks the Current Location link, on pageAnimationEnd I'd like to load a page and append it to #currentloc. 我想做的是,当用户单击pageAnimationEnd上的“当前位置”链接时,我想加载页面并将其附加到#currentloc。 Here's my code: 这是我的代码:

$('#currentloc').bind('pageAnimationEnd', function(e, info){
    $(this).load('results.php');
});

As you can see, this will entirely replace the contents of #currentloc. 如您所见,这将完全替换#currentloc的内容。 I'm not sure how I append the content of results.php without replacing what's already there. 我不确定如何附加results.php的内容而不替换已经存在的内容。 I've looked at putting the ajax load request inside of append: 我看过将ajax加载请求放在append内:

$(this).append(load('results.php'));

but that totally wasn't working... 但这完全没有用...

Any idea what to do? 知道该怎么办吗?

UPDATE UPDATE

One thing I've tried which works but feels a little clumsy is 我尝试过的一件事奏效,但感觉有点笨拙是

$('#currentloc').bind('pageAnimationEnd', function(e, info){
 $(this).append('<ul></ul>');  
 $(this).find('ul').load('results.php');
});

Feel like there's a better solution to this. 感觉有更好的解决方案。

The load function in jquery replaces the content, so you must create another element if you wish to keep the current content. jquery中的load函数将替换内容,因此,如果要保留当前内容,则必须创建另一个元素。 You can could make your code a little shorter if you remove the call to the find function. 如果删除对find函数的调用,则可以使代码短一些。

$('#currentloc').bind('pageAnimationEnd', function(e, info){
   $(this).append($('<ul />').load('results.php'));   
});

or 要么

$('#currentloc').bind('pageAnimationEnd', function(e, info){
   $('<ul />').load('results.php').appendTo(this);   
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM