简体   繁体   English

如何在不重新加载网页的情况下更改div的内容?

[英]How to change the content of a div without reloading the webpage?

I have a website, that uses PHP to select the content, 我有一个网站,该网站使用PHP选择内容,

<div>  
    <? include ("navigation.php"); // navigation.php generates the menu ?>
</div>
<div>
    <?      
        $type = $_GET["type"];
        switch ($type) {
        case "page" :
            include "Text.php";
            break;  
        case "news":
            include "news_2.php";
            break;
        default :
            include "main.php";     
        }
    ?>
</div>

The url is of the format domain.com/index.php?type . 网址的格式为domain.com/index.php?type I need to change the block #content without reloading the whole page, how can I do this? 我需要在不重新加载整个页面的情况下更改块#content ,我该怎么做?

As you've tagged the question with "jquery" I assume you know what that is, and that you're loading it into your page. 当您用“ jquery”标记问题时,我假设您知道这是什么,并且您正在将其加载到页面中。

All you need to is give your div and ID... content here 您需要提供的div和ID ...这里的内容

And then use a bit of jquery.. in its simplest form just to load your content from 'myurl.php' into 'mydiv' when the page has finished loading: 然后在页面完成加载后,以最简单的形式使用一些jquery ..只是将您的内容从“ myurl.php”加载到“ mydiv”:

$(document).ready(function() { 
  $("#mydiv").load("myurl.php");
});

You'll no doubt want some logic to determine what loads, and under what circumstances. 毫无疑问,您将需要一些逻辑来确定什么负载以及在什么情况下。 If you need to pass data back to the URL then you'll need to go for jquery ajax ($.ajax). 如果您需要将数据传递回URL,则需要使用jquery ajax($ .ajax)。 Its all pretty easy, loads of examples on the web, and good docs on the JQuery website. 一切都很简单,网络上有大量示例,JQuery网站上有不错的文档。

This would best be done with Ajax. 最好使用Ajax完成。 I like using jQuery's ajax function. 我喜欢使用jQuery的ajax函数。 Something like this: 像这样:

function load(page){    
    var datastring='ANY DATA YOU WANT TO SEND';

    $.ajax({
        type: "POST",
        url: 'your/pagehtml/',
        data: "bust="+Date()+datastring,
        dataType: "html",
        cache: false,
        success: function(html){ 
            $('#content').html(html)
        }
    });
    return false;
}

You wouldn't need to send the page in the URL this way. 您无需以这种方式通过URL发送页面。 Anytime you change the url, you must be loading a different page. 每当您更改网址时,您都必须加载其他页面。 Outside of .htaccess rewrite. 在.htaccess之外重写。 Which isn't what you need. 这不是您所需要的。

Fire this on click or whatever you want. 单击或任何您想要的触发。

If you're using jQuery, it's pretty easy. 如果您使用的是jQuery,则非常简单。 You didn't post what is supposed to trigger the change, so I'll assume you have a list of links in another element with an id of nav. 您没有发布应该触发更改的内容,因此我假设您在另一个元素中具有id为nav的链接列表。

Read more about the jQuery Ajax request here: http://api.jquery.com/jQuery.ajax/ 在此处阅读有关jQuery Ajax请求的更多信息: http : //api.jquery.com/jQuery.ajax/

//run on page load
$(function(){   
    //bind a click event to the nav links
    $("#nav a").bind("click", function(e){ 

        //keep the links from going to another page by preventing their default behavior
        e.preventDefault();

        //this = link; grab the url
        var pageLocation = this.href;

        //fire off an ajax request
        $.ajax({ 
            url: pageLocation, 

            //on success, set the html to the responsetext
            success: function(data){ 
                $("#content").html(data.responseText); 
            } 
        });
    });
});

I'd also suggest doing some code cleanup like caching your $("#content") element on the load event (something like window.container = $("#container"), and using window.container later on), but I left it as-is so that everything remains clear. 我还建议做一些代码清理工作,例如在load事件上缓存$(“#content”)元素(类似于window.container = $(“#container”),然后在以后使用window.container),但是我保持原样,以便一切保持清晰。

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

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