简体   繁体   English

如何在不刷新整个页面的情况下使用jQuery自动重新加载页面的一部分?

[英]How do I auto-reload a section of the page with jQuery without refreshing entire page?

So there are a few things I want to do. 所以我想做几件事。 I have a section of HTML that I would like to be displayed on the page by default (ie the first time it loads). 我有一段HTML,默认情况下(即第一次加载时)希望在页面上显示。

<div class="viewport">
        <ul class="overview">           
            <li><img src="images/1.png" /></li>
            <li><img src="images/2.png" /></li>
            <li><img src="images/3.png" /></li>         
            <li><img src="images/4.png" /></li>
            <li><img src="images/5.png" /></li>
            <li><img src="images/6.png" /></li>
            <li><img src="images/7.png" /></li>         
        </ul>
    </div>

However, upon clicking an icon I would like for this section to be reloaded as a snippet of javascript is updated. 但是,单击图标后,我希望此部分随着javascript代码段的更新而重新加载。

The code for the icon is: 图标的代码为:

<a href="#"><img src="images/2.png" id="icon#2"></a> | 
<a href="#"><img src="images/3.png" id="icon#3"></a> | 
<a href="#"><img src="images/4.png" id="icon#4"></a> | 

The JS snippet that is updated is: 更新的JS代码段是:

var win_width = $(window).width();
var num_of_images = 2; // This should be changed to 3, when 3 is clicked, and 4 when 4 is clicked, etc.

So it should reload the 'viewport' div on the page only...not the entire page. 因此,它应该仅在页面上重新加载“ viewport” div,而不是整个页面。

Thoughts? 有什么想法吗?

    $(function () {

        var num_of_images = 2,
            showAndHide;

        $("img[id^=icon]").bind("click", function () {
            num_of_images = parseInt(this.id.replace("icon#", ''), 10);
            showAndHide(num_of_images);
        });

        (showAndHide = function (n) {
            $("ul.overview").hide() // hide this while we "do work"
               .children() // get children
                  .show() // show all children
                  .filter(":nth-child(" + n + ") ~ li") // get the next siblings of the nth child
                     .hide() // hide them
                  .end()
               .end()
            .show(); // show the parent list
        })(num_of_images)

    });

This is a "pending" solution. 这是一个“待定”的解决方案。 I have not tested this and I would be surprised if it works. 我没有对此进行测试,如果它起作用我会感到惊讶。

EDIT : Alright, I tested and fixed a few things and this seems to work. 编辑 :好的,我测试并修复了一些问题,这似乎可行。

Note that I attached a click handler to those img elements. 请注意,我在这些img元素上附加了点击处理程序。 So you don't need those anchor tags if you don't want them. 因此,如果您不需要这些锚标记,则不需要它们。

如果您只想用HTML内容重新加载页面的一部分(不添加JavaScript),则可以使用.load()

$('div#content').load('morecontent.php');

You want $('.viewport').load('/url') to replace the viewport' contents with the new chunk of HTML return from the server from /url. 您希望$('.viewport').load('/url')用服务器从/ url返回的HTML新块替换视口的内容。 You call that load function once when the page loads, and again any time you want to replace the contents of viewport. 您在页面加载时调用一次该加载函数,然后在您要替换视口的内容时再次调用该加载函数。 You might want to do some include type magic in whatever template language you're using to make it so that you can skip the initial load and render the initial page in one request instead of 2. You trigger the load function in a change event or other kind of event on your page. 您可能想要在使用的任何模板语言中进行一些包含type magic的操作,以便可以跳过初始加载并在一个请求(而不是2)中呈现初始页面。在change事件中触发load函数或页面上的其他事件。

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

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