Yii registerScript的简单运用

本文详细介绍了Yii框架中CClientScript类的registerScript()方法使用方法。该方法用于注册JavaScript代码,支持不同的加载位置,如页面头部、开始、结束等,并提供了一个示例展示如何在页面加载完成后执行进度条动画。

registerScript()方法
public CClientScript registerScript(string $id, string $script, integer $position=4)
$idstringID that uniquely identifies this piece of JavaScript code //代码片段的标示字段
$scriptstringthe javascript code //显示在页面里面的代码
$positionintegerthe position of the JavaScript code. Valid values include the following: //加载方式
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
  • CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
  • CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
{return}CClientScriptthe CClientScript object itself (to support method chaining, available since version 1.1.5).

视图页面加载

Yii::app()->clientScript->registerScript('scriptId', "
	var count = 0;
	var step  = 10;
	var speed = 500;
	function progress() {
		$('#amount').text(count+'%');
		$('#progress').progressbar('option', 'value', count);
		if(count < 100) {
			count = count+step;
			setTimeout(progress, speed);
		}
	}
	progress();
", CClientScript::POS_LOAD);

显示效果

jQuery(window).load(function() {

	var count = 0;
	var step  = 10;
	var speed = 500;
	function progress() {
		$('#amount').text(count+'%');
		$('#progress').progressbar('option', 'value', count);
		if(count < 100) {
			count = count+step;
			setTimeout(progress, speed);
		}
	}
	progress();

});


测试2

Yii::app()->clientScript->registerScript('script_id1','
	function test(){
		alert("测试内容");		
	}
	test();
',CClientScript::POS_LOAD);




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值