jQuery Ajax 加载 url 的片段,该片段由变量定义

发布于 2024-11-26 14:36:51 字数 2438 浏览 1 评论 0原文

相对较新的 jQuery 用户(曾经使用过它,但现在真正尝试理解它)。

我正在尝试使用 ajax 创建我自己的 jQuery 分页,到目前为止我的代码如下所示:

$(document).ready(function(){
    $('.pigs ul.pigspagination li a').live('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if(url.match('^http')){
            return true;
        } else {
            var element = $(this).closest(".pigs");
            $(element).append('<div id="loading"><img alt="Loading..." src="loading.gif" /></div>');
            $('#loading').fadeIn('normal');
            $(element).load(url + ' ' + element);
            return false;
        }
    });
});

代码基本上是在单击“.pigs ul.pigspagination li a”时加载父元素,即给定 url 中的“.pigs”然后从 url 加载相同的片段。然而,这似乎不起作用:

$(element).load(url + ' ' + element);

我假设正在加载网址,然后添加一个空格,然后说明引用的元素 var element = $(this).closest(".pigs");

如果你想知道为什么我不能放 $(element).load(url + ' .pigs');

这是因为我多次引用 .pigs ,并且它是自动的,因为它是我编写的 php gallery 脚本。

html 看起来大致是这样的:

<div class="pigs">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="pagination link">&lt; Prev</a></li>
        ...
        <li><a href="pagination link">Next &gt;</a></li>
    </ul>
</div>

无论如何加载更多的画廊。

有什么想法吗?

--------------编辑bfavaretto-------------- 尝试了这个

var frag = $(this).closest(".pigs").attr('id');
$(element).load(url + ' #' + frag);

,它只加载了吸力,但删除了所有代码,我的猜测是碎片传递得很糟糕。 .attr('id');会自己获取 id,而不是“#”,对吧?

如果有人感兴趣的话结局代码:

$(document).ready(function(){
    $('.pigs ul.pigspagination li a').live('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if(url.match('^http')){
            return true;
        } else {
            var element = $(this).closest(".pigs");
            $(element).append('<div id="loading"><img alt="Loading..." src="loading.gif" /></div>');
            $('#loading').fadeIn('normal');
            var frag = $(this).closest(".pigs").attr('id');
            $(element).load(url + ' #' + frag);
            return false;
        }
    });
});

relatively new jQuery user(used it in the pass but really trying to understand it now).

Am trying to create my own jQuery pagination with ajax, so far my code looks like so:

$(document).ready(function(){
    $('.pigs ul.pigspagination li a').live('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if(url.match('^http')){
            return true;
        } else {
            var element = $(this).closest(".pigs");
            $(element).append('<div id="loading"><img alt="Loading..." src="loading.gif" /></div>');
            $('#loading').fadeIn('normal');
            $(element).load(url + ' ' + element);
            return false;
        }
    });
});

The code basically says on click of ".pigs ul.pigspagination li a" load the parent element which is ".pigs" from the the given url then load the same fragment from the url. However this does not seem to be working:

$(element).load(url + ' ' + element);

I assume that am loading the url then am adding a space then saying for the element which is referenced from
var element = $(this).closest(".pigs");

if your wondering why i just cant put
$(element).load(url + ' .pigs');

It is because i reference .pigs multiple times and its automatic since it the php gallery script i wrote.

The html looks roughly like this:

<div class="pigs">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="pagination link">< Prev</a></li>
        ...
        <li><a href="pagination link">Next ></a></li>
    </ul>
</div>

times however many more galleries are loaded.

Any ideas?

--------------EDIT for bfavaretto--------------
Tried this

var frag = $(this).closest(".pigs").attr('id');
$(element).load(url + ' #' + frag);

it loads just that suction but removes all the code, my guess is frag is being passed badly. the .attr('id'); will get the id by its self, not the '#' right?

Finale code if anyone is interested:

$(document).ready(function(){
    $('.pigs ul.pigspagination li a').live('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if(url.match('^http')){
            return true;
        } else {
            var element = $(this).closest(".pigs");
            $(element).append('<div id="loading"><img alt="Loading..." src="loading.gif" /></div>');
            $('#loading').fadeIn('normal');
            var frag = $(this).closest(".pigs").attr('id');
            $(element).load(url + ' #' + frag);
            return false;
        }
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

差↓一点笑了 2024-12-03 14:36:51

经过我们对评论的讨论,我想我明白你想要什么。

您的示例网址 index.php?pigsgallery=1&pigspage=1 看起来不错。我假设它只会返回 Pigsgallery X 的内容,在第 Y 页上。完美。

因此,您的问题似乎是将 URL 返回的内容插入到右侧库 div 中,即包含单击的分页链接的库。你快到了。正如我在其中一条评论中建议的那样, $(element).load(url) 应该是单词。它的意思是“加载 url,并将 $(element) 的内容替换为响应”。

您的 HTML 应该如下所示(两个图库当前都位于第 2 页):

<div class="pigs" id="pigs1">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="index.php?pigsgallery=1&pigspage=1">< Prev</a></li>
        ...
        <li><a href="index.php?pigsgallery=1&pigspage=3">Next ></a></li>
    </ul>
</div>

<div class="pigs" id="pigs2">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="index.php?pigsgallery=2&pigspage=1">< Prev</a></li>
        ...
        <li><a href="index.php?pigsgallery=2&pigspage=3">Next ></a></li>
    </ul>
</div>

如果您的 HTML 不是这样的,您可能会混淆什么应该在 PHP 中完成,什么需要 jQuery。 PHP 应该处理该请求并仅返回您需要在其中一个 div 上替换的内容。然后应该使用 jQuery 将该内容实际插入到页面的右侧元素内(您已经使用 var element = $(this).closest(".pigs") 获得了该内容)。

After what we dicussed on the comments, I think I understand what you want.

Your example URL index.php?pigsgallery=1&pigspage=1 looks good. I assume it will only return the contents of pigsgallery X, on page Y. Perfect.

So your problem seems to be inserting the contents returned by the URL into the right gallery div-- the one which contains the pagination link clicked. You are almost there. As I suggested on one of the comments, $(element).load(url) should word. It means "load the url, and replace the contents of $(element) with the response".

Your HTML should look like the following (with both galleries currently on page 2):

<div class="pigs" id="pigs1">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="index.php?pigsgallery=1&pigspage=1">< Prev</a></li>
        ...
        <li><a href="index.php?pigsgallery=1&pigspage=3">Next ></a></li>
    </ul>
</div>

<div class="pigs" id="pigs2">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="index.php?pigsgallery=2&pigspage=1">< Prev</a></li>
        ...
        <li><a href="index.php?pigsgallery=2&pigspage=3">Next ></a></li>
    </ul>
</div>

If this is not how your HTML looks like, you may be confusing what should be done in PHP and what requires jQuery. PHP should handle the request and return just the content you need to replace on one of the divs. Then jQuery should be used to actually insert that content into the page, inside the right element (which you already have with var element = $(this).closest(".pigs").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文