简体   繁体   English

jQuery + AJAX动态div内容

[英]JQuery + AJAX dynamic div content

I'm trying to create a dynamic web page to show some content and change it without reloading the full page but some part of it. 我正在尝试创建一个动态网页来显示某些内容并对其进行更改,而不重新加载整个页面的一部分。

When i press a button of my navigation bar it calls a JavaScript function to change the content of my div tag 当我按下导航栏的按钮时,它会调用JavaScript函数来更改div标签的内容

<div class="row text-left" id="center"></div>

with this code i put into my div class some text 使用此代码,我在div类中添加了一些文本

$('#navHome').click(function(){
    $('#center').html("this is my home");
});

Now i want store a content of every section to reload this every time when i press the button. 现在,我想存储每个部分的内容,以便在每次按下按钮时重新加载。 Something like this: 像这样:

$('#navHome').click(function() {
   $.get('home.txt', function(data) {
      $("#center").html(data);
   }, 'text');
});

in the home.txt i have store my html code of home section but i have no idea how to make this. 在home.txt中,我已经存储了我的home部分的html代码,但是我不知道该如何做。

Try: 尝试:

Remove 'text' which you added in last of ajax 删除您在ajax最后添加的“文本”

$('#navHome').click(function(){
   $.get('home.txt', function(data) {
      alert(data);
      //process text file line by line
      $('#center').html(data);
   });
});

I will try to give you an answer, first at all sorry for my poor english. 我会尽力给你一个答案,首先要对我的英语不好对不起。

My first recommendation for your case is to use AngularJS or any similar framework it´s designed to create SPA(Simple Page Application) which basically means what you want, load te full page one time and only change some part of the page when the user request it. 对于您的情况,我的第一个建议是使用AngularJS或它设计用来创建SPA(简单页面应用程序)的任何类似框架,这基本上意味着您想要的内容,一次加载整个页面,并且仅在用户使用时更改页面的某些部分要求它。 It also has a lot of features that will help you to code faster and cleaner. 它还具有许多功能,可帮助您更快更干净地进行编码。

Now, I'm going to write some piece of code on simple jquery if you dont want to learn angular now :) 现在,如果您现在不想学习angular,我将在简单的jquery上写一些代码:)

function includeURL(idObj,url){
   var r=$.Deferred();  
   $(document).ready(function() {
      $(idObj).load(url, function() {
          return r.resolve();
      });
   });
}

I use this function to load parts of code, idObj is the div where you want to load the content and url the path of the content. 我使用此函数来加载部分代码,idObj是要在其中加载内容并为内容的路径添加URL的div。 An example could be: 例如:

includeURL('#myDiv','html/path/to/content.html');

Try it and tell me if you have any problem. 试试看,告诉我您是否有任何问题。

Hope it helps! 希望能帮助到你!

Assuming the contents of the home.txt file can just be placed directly into the <div /> element (ie it's just plain text or HTML), have a look at the .load() function within jQuery 假设home.txt文件的内容可以直接放入<div />元素(即纯文本或HTML),请查看jQuery中的.load() 函数

$('#center').load('home.txt', function() {
  // This function is called when the contents are loaded successfully
});

So your click handler would look something like this: 因此,您的点击处理程序将如下所示:

$('#navHome').click(function() {
    $('#center').load('home.txt');
});

Make sure that home.txt is in the correct directory. 确保home.txt在正确的目录中。 The 'networking' tab available in the developer tools for all good web browsers will help you debug problems if the contents doesn't load correctly 如果内容加载不正确,开发人员工具中适用于所有优质Web浏览器的“联网”标签将帮助您调试问题

try use the following code: 尝试使用以下代码:

 $.ajax({ url: "home.txt", dataType: "text" }) .done(function( data ) { $("#center").replaceWith(data); }); 
To replace all div content, use replaceWith(data) method To Append div content apply append(data) method; 要替换所有div内容,请使用replaceWith(data)方法。 You'll get the same result using $get method, but must to assign the dataType as 'text'; 使用$ get方法将得到相同的结果,但是必须将dataType分配为'text';

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

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