CodeIgniter and Javascript/Jquery Library

前端 未结 9 843
粉色の甜心
粉色の甜心 2021-02-12 11:51

As title said, I\'m trying to figure out how to use javascript and jquery libraries on CI.

Following instruction in the docs, I load the library in my controller:

相关标签:
9条回答
  • 2021-02-12 12:21

    Use:

    $this->load->library('javascript/jquery');
    

    instead of:

    $this->load->library('jquery');
    

    This will load your jQuery library.

    0 讨论(0)
  • 2021-02-12 12:25

    Put the code in the config.php like this:

    $config['javascript_location'] = 'js/jquery/jquery.js';
    $config['javascript_ajax_img'] = 'images/ajax-loader.gif';
    

    In your controller file (e.g. controllers/sample.php) type this codes:

     function __construct()
       {
            parent::__construct();
                $this->load->library('javascript');                    
       }
    
    function index()
    {
    
        $data['library_src'] = $this->jquery->script();
        $data['script_head'] = $this->jquery->_compile();
    
        $this->load->view('sampleview', $data);
    
    }
    

    In your view file (e.g. views/sampleview.php) type this codes:

    <?php echo $library_src;?>
    <?php echo $script_head;?>
    

    This works for me. I hope it works for you too. XD

    0 讨论(0)
  • 2021-02-12 12:26

    You can try this, it's work to me.
    in config.php

    $config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';
    

    in Controller

     public function __construct() {
        parent::__construct();
        $this->load->library('javascript');
        $this->load->library('javascript/jquery');
    }
    
     public function index(){
        $d['library_src'] = $this->jquery->script();
        $d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');");
        $this->load->view('main',$d);
     }
    

    in view (head)

    <?php echo $library_src; ?>
    

    in view content (body)

    <div class="alert alert-dismissable alert-info" id="logalert">                          
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        </div>
    

    in javascript view

    <script type="javascript">
    $(document).ready(function(){ 
       <?php echo $logalert; ?>
    };
    </script>
    

    happy coding :)

    0 讨论(0)
提交回复
热议问题