1

来自http://www.yiiframework.com/forum/index.php/topic/37313-confused-about-jquery-and-script-inclusion-handling/的交叉发布,但有时人们在这里回答得更快......

我对 Yii 对 jquery 和其他 javascript 文件包含的处理感到非常困惑。

这是有效的:

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#Event_recuring_nature, #Event_venue, #Event_expected_attendance, #Event_age_group, #Event_state, #Event_gender, #Event_ethnicity, #Event_exclusivity, #Package_cash_product_sponsorships, #Event_vanue_signature, #Event_logo_on_step_and_repeat, #Event_inclusion_in_press, #Event_ad_space, #Event_inclusion_in_event_materials, #Package_category_exclusivity, #logo_on_signature, #Event_proposed_events_included, #Event_dependant_on_sponsorship, #Event_sponsorship_type, #Event_attendee_professions, #Event_attendee_income').yaselect();
    });
</script>
<script type="text/javascript" src="/js/libs/jquery-ui-datetimepicker.js"></script>
<script type="text/javascript" src="/js/event_create.js"></script>
<script type="text/javascript" src="/js/slider.js"></script>
<script type="text/javascript" src="/js/other_social.js"></script>
<script type="text/javascript" src="/js/package.js"></script>

而这(据说会更好?)没有:

<?php 
Yii::app()->clientScript->registerScript('create-script', "
    $(function()  {
          $('body').css('overflow-x','hidden');
        $('#Event_recuring_nature, #Event_venue, #Event_expected_attendance, #Event_age_group, #Event_state, #Event_gender, #Event_ethnicity, #Event_exclusivity, #Package_cash_product_sponsorships, #Event_vanue_signature, #Event_logo_on_step_and_repeat, #Event_inclusion_in_press, #Event_ad_space, #Event_inclusion_in_event_materials, #Package_category_exclusivity, #logo_on_signature, #Event_proposed_events_included, #Event_dependant_on_sponsorship, #Event_sponsorship_type, #Event_attendee_professions, #Event_attendee_income').yaselect();
    });

");
?>
<?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/libs/jquery-ui-datetimepicker.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/event_create.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/slider.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/other_social.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/package.js'); ?>

我有 <app_dir>/js/所有 javascript 的目录,并 <app_dir>/protected/assets/js按照 yii 论坛上的 registerScriptFile 帖子中的建议创建(据说可以很好地处理脚本包含)。我当然有重复的文件,我正在清理。

布局在标题中有这个:

    <?php Yii::app()->clientScript->registerCoreScript('jquery')?>
    <?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/libs/jquery-ui-1.8.20.custom.min.js')?>
    <?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/libs/jquery.yaselect.min.js')?>
    <?php Yii::app()->clientScript->registerScriptFile($this->assetsBase.'/js/script.js')?>

我在 config/main.php 中有这个:

// application components
   'components'=>array(
      'clientScript'=>array(
            'packages'=>array(
                 'jquery'=>array(
                     'baseUrl'=>Yii::app()->request->baseUrl . '/js/libs/',
                     'js'=>array('jquery-1.7.2.min.js')
                  )                                       
             )
      ),

(我知道 baseUrl 现在指向 /js,但尝试使用

'baseUrl'=>Yii::app()->basePath . '/assets/js/libs' 

也根本没有加载 jquery !!!

请帮忙!我是 Yii 的新手,对此我失去了理智。谢谢你。

编辑:这是代码生成的:

<script type="text/javascript" src="/js/libs/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/assets/ab20866e/jquery.yiiactiveform.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/libs/jquery-ui-datetimepicker.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/event_create.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/slider.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/other_social.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/package.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/libs/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/libs/jquery.yaselect.min.js"></script>
<script type="text/javascript" src="/assets/2dfe5520/js/script.js"></script>

对我来说,第二行 /assets/ab20866e/jquery.yiiactiveform.js 看起来很可疑,因为它具有不同的资产 ID...

4

2 回答 2

3

虽然我面前没有您的布局文件,但您的布局似乎$content在脚本文件注册之前已经存在。

因此,视图中的脚本被加载,然后是模板中的脚本。发生这种情况时,日期时间选择器正在加载,但由于 jquery-ui 尚未加载而报告错误。确保您的模板之前加载了脚本$content

或者,您可以通过指定位置参数来强制加载在视图中的脚本在正文中,而不是在头部中,例如:

<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/libs/jquery-ui-datetimepicker.js', CClientScript::POS_BEGIN); ?>
于 2012-11-07T16:53:23.460 回答
0

要考虑的另一件事是强制 Yii 从一开始就加载 jQuery,如下所示:

cs()->registerCoreScript('jquery');

你也可以像这样加载 jQuery UI:

cs()->registerCoreScript('jquery.ui');

我发现这在加载其他 jQuery 库和文件时解决了很多registerScriptFile POS_BEGIN/POS_HEAD 问题(如 ernies 回答)

于 2012-11-07T20:32:54.910 回答