用另一个控制器视图中的JQuery替换div

时间:2011-04-21 12:53:20

标签: jquery codeigniter

想象一下,我有以下通过一堆视图生成的'部分':

<div id="funstuff">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>

通过使用codeigniter,我可以重用视图(我做了很多),并且我想用另一个视图中的内容动态替换'funstuff'div。它的控制器方法仍然属于同一类:

class FunnyThings extends CI_Controller {

   public function index() {
   $this->load->view('funpage_view');
   }

   public function funview() {
   $this->load->view('fun_partial_view');
   }
}

基本上,使用Jquery,我可以用不同的内容替换该视图:

$('#funstuff').replaceWith('<div><h2>New heading</h2></div>');

但是,如何动态地从其他视图中获取内容并将其替换为我选择的div?

谢谢,

2 个答案:

答案 0 :(得分:2)

jQuery为此提供了一个名为load()的便捷方法,有关更多信息,请参阅http://api.jquery.com/load/

在你的情况下,该行将是这样的:

$('#funstuff').load('FunnyThings/funview');

其中FunnyThings/funview是控制器的相对URL。

答案 1 :(得分:1)

假设您知道其他视图的idclass,您可以这样做:

// if you know the class name (other-view)
    $('#funstuff').replaceWith($(".other-view")); 

// if you know the id (other-view)    
    $('#funstuff').replaceWith($("#other-view"));