如何在div-codeigniter中加载视图

如何在div-codeigniter中加载视图,codeigniter,view,load,Codeigniter,View,Load,我有一个页面,我想保持静态,有两个div,我想在用户导航期间加载不同的html视图。我希望更改内容而不刷新整个页面,只刷新这两个div 如何将特定视图加载到这些div中? 我已经有了菜单导航,可以确定要加载到那些div中的html视图和控制器函数,以及使用javascript获得的菜单选项,然后在主页的控制器中使用它,我只是不知道如何加载内容 您可以使用一点Javascript和AJAX加载视图 使用jQuery: $('a.nav-button').click(function(e) {

我有一个页面,我想保持静态,有两个div,我想在用户导航期间加载不同的html视图。我希望更改内容而不刷新整个页面,只刷新这两个div

如何将特定视图加载到这些div中?
我已经有了菜单导航,可以确定要加载到那些div中的html视图和控制器函数,以及使用javascript获得的菜单选项,然后在主页的控制器中使用它,我只是不知道如何加载内容

您可以使用一点Javascript和AJAX加载视图

使用jQuery:

$('a.nav-button').click(function(e) {
  // prevent the default action when a nav button link is clicked
  e.preventDefault();

  // ajax query to retrieve the HTML view without refreshing the page.
  $.ajax({
    type: 'get',
    url: '/path/to/your/controller/method',
    dataType: 'html',
    success: function (html) {
      // success callback -- replace the div's innerHTML with
      // the response from the server.
      $('#yourDiv').html(html);
    }
  });
});

为了在不刷新的情况下放置内容,您应该使用ajax,假设您在目录(webparts)下的文件中有div,并且您希望将其内容添加到名为my_div的div中;首先在控制器上创建一个方法,该方法只渲染要放置的视图(div),如下所示

function part($part = false)
{
   $data = array(); // you can add data to your div, if you need to.
   $this->load->view("webparts/$part", $data);
}
 $('#my_div').load('controller/mypart #certainsection');
然后使用javascript(本例中为jQuery)可以调用

$('#my_div').load('controller/mypart');
注意:我喜欢使用“加载”,因为它允许我选择我请求的视图的某些部分,使用类似这样的方式

function part($part = false)
{
   $data = array(); // you can add data to your div, if you need to.
   $this->load->view("webparts/$part", $data);
}
 $('#my_div').load('controller/mypart #certainsection');

将使用mypart视图中的id=“certainsection”加载元素

我认为您需要使用jquery和Codeigniter

function method(){

   $data['result'] = ''//you can use this if you need to pass some data to the view


   print $this->load->view('your_view',$data,true);//This will load your view page to the div element 

}
这将是您的jQuery代码

$('nav').click(function(){//element to be click to load the page in the div
     $(your_div_element).load('controller/method');

});
这将是你的代码点火器

function method(){

   $data['result'] = ''//you can use this if you need to pass some data to the view


   print $this->load->view('your_view',$data,true);//This will load your view page to the div element 

}

祝你好运

如何传递值/变量?返回而不是打印