2014-12-27 80 views
-1

如何在yii 2.0的mi視圖中添加jquery腳本?我在尋找,我發現這個命令jquery腳本yii2

$this->registerJs('$(function() 
    { 
     $("#wizard").steps({ 
      headerTag: "h2", 
      bodyTag: "section", 
      transitionEffect: "slideLeft" 
     }); 

     $("#wizard-vertical").steps({ 
      headerTag: "h2", 
      bodyTag: "section", 
      transitionEffect: "slideLeft", 
      stepsOrientation: "vertical" 
     }); 
    });', 3); 

和輸出是這樣的,一個JavaScript

<script type="text/javascript">$(function() 
    { 
     $("#wizard").steps({ 
      headerTag: "h2", 
      bodyTag: "section", 
      transitionEffect: "slideLeft" 
     }); 

     $("#wizard-vertical").steps({ 
      headerTag: "h2", 
      bodyTag: "section", 
      transitionEffect: "slideLeft", 
      stepsOrientation: "vertical" 
     }); 
    });</script> 

的問題是我需要把它作爲一個jQuery腳本這樣

<script> 
     $(function() 
     { 
      $("#wizard").steps({ 
       headerTag: "h2", 
       bodyTag: "section", 
       transitionEffect: "slideLeft" 
      }); 

      $("#wizard-vertical").steps({ 
       headerTag: "h2", 
       bodyTag: "section", 
       transitionEffect: "slideLeft", 
       stepsOrientation: "vertical" 
      }); 
     }); 
    </script> 

但我不知道如何打印JQuery腳本,有人知道嗎?

+2

2幾乎相當。什麼似乎是問題? – MasterAM 2014-12-27 16:49:16

+0

這個問題沒有意義.... – 2014-12-28 11:42:23

+0

jQuery是JS,所以它的語法正確。如果它不工作,請確保jQuery文件已加載,並且在jQuery加載後執行JS。 – 2014-12-28 12:41:27

回答

3

你可以使用這個....

更新或包括app\assets\AppAsset.php文件

class AppAsset extends AssetBundle 
{ 
    public $basePath = '@webroot'; 
    public $baseUrl = '@web'; 
    public $css = [ 
     'css/site.css', 
    ]; 
    public $js = [ // add required js file.... 
    'js/jquery.min', 
    'js/jquery-scrollToTop', 

    ]; 
    public $jsOptions = [ 
    'position' => \yii\web\View::POS_HEAD // include js at <head> tag.... 
    ]; 
    public $depends = [ 
     'yii\web\YiiAsset', 
     'yii\bootstrap\BootstrapAsset', 
    ]; 
} 

這樣的jQuery代碼,我希望這個答案幫助你......... :)