[英]How to pass jQuery scripts with codeIgneter
My goal is to pass a jQuery script through a model with CodeIgneter. 我的目标是通过带有CodeIgneter的模型传递jQuery脚本。
This is the view where I linked the jquery.js file 这是我链接jquery.js文件的视图
<footer>
<p>© Company 2014</p>
</footer>
<script src="<?php echo base_url('assets/js/jquery.js');?>"> </script>
</div> <!-- /container -->
This is the model jquery 这是模型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>';
}
}
In the controller I passed the script as following: 在控制器中,我通过了如下脚本:
$this->load->model('jquery');
$data['testjs'] = $this->jquery->test();
$this->load->view('login/content',$data);
In the content view I echo the script: 在内容视图中,我回显脚本:
...echo $testjs...
Unfortunately the script does not work as expected. 不幸的是,脚本无法按预期工作。
you need to include jquery library. 您需要包括jquery库。 Add this in your header view
在标题视图中添加
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
and then this code in somewhere in view or header view 然后将此代码放在视图或标题视图中的某个位置
jQuery(document).ready(function() {
jQuery("#submitForm").click(function(){
alert("this is working file ");
});
});
you can use Dom crawler for that if you want to send html and jquery data pass through controller 如果您想通过控制器发送html和jquery数据,则可以使用Dom搜寻器
https://github.com/dimabdc/PHP-Fast-Simple-HTML-DOM-Parser https://github.com/dimabdc/PHP-Fast-Simple-HTML-DOM-Parser
or else you can set in header view file as above answer given 否则您可以在标题视图文件中设置以上给出的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.