[英]How to pass jQuery scripts with codeIgneter
我的目標是通過帶有CodeIgneter的模型傳遞jQuery腳本。
這是我鏈接jquery.js文件的視圖
<footer>
<p>© Company 2014</p>
</footer>
<script src="<?php echo base_url('assets/js/jquery.js');?>"> </script>
</div> <!-- /container -->
這是模型jQuery
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Jquery extends CI_Model {
public function test(){
return
'<script>
$(document).ready(function() {
$("#submitForm").click(function(){
alert("test");
});
});
</script>';
}
}
在控制器中,我通過了如下腳本:
$this->load->model('jquery');
$data['testjs'] = $this->jquery->test();
$this->load->view('login/content',$data);
在內容視圖中,我回顯腳本:
...echo $testjs...
不幸的是,腳本無法按預期工作。
您需要包括jquery庫。 在標題視圖中添加
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
然后將此代碼放在視圖或標題視圖中的某個位置
jQuery(document).ready(function() {
jQuery("#submitForm").click(function(){
alert("this is working file ");
});
});
如果您想通過控制器發送html和jquery數據,則可以使用Dom搜尋器
https://github.com/dimabdc/PHP-Fast-Simple-HTML-DOM-Parser
否則您可以在標題視圖文件中設置以上給出的答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.