【问题标题】:How can i use php jquery in yii2?如何在 yii2 中使用 php jquery?
【发布时间】:2015-08-29 07:13:49
【问题描述】:

我想让一个图像在我点击它时成为一个弹出窗口。这就像一个更大的默认大小预览,而无需移动到另一个页面。我有这个适用于 PHP 的 jQuery,但由于这是 yii2,所以我被困住了。

这里是 jQuery:

<script>        
     jQuery(window).load(function() {   
     $x = $(window).width();        
if($x > 1024)
{           
jQuery("#content .row").preloader();    }            
 jQuery('.magnifier').touchTouch();
     jQuery('.spinner').animate({'opacity':0},1000,'easeOutCubic',function (){jQuery(this).css('display','none')}); 
      }); 

</script>

我想在这个视图中实现那个 jQuery:

    <?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\bootstrap\Modal;
use frontend\models\PengurusAktif;
use frontend\models\ProfilBem;

/* @var $this yii\web\View */
/* @var $searchModel frontend\models\AlbumBemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Album Bem';
?>

<div id='content' class="album-bem-index well">
    <?php
    $tahun = PengurusAktif::find()->one();
    $idprofil = ProfilBem::find()->where(['tahun' => $tahun->tahun_aktif])->one();
    ?>
    <?= Html::a('Visi Misi', ['/bem/visimisi', 'id' => $tahun->tahun_aktif], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Struktur Organisasi', ['profil-bem/profilebem', 'id' => $idprofil->id_profil_bem], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Pengumuman BEM', ['/pengumuman/pengumumanbem'], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Event Bem', ['/event/eventbem'], ['class' => 'btn btn-primary']) ?>
    <?php
    if (Yii::$app->user->can('sekretarisBem')) {
        echo Html::a('Forum BPH BEM', ['/forum/forumbem'], ['class' => 'btn btn-primary']);
    }
    ?>
    <?= Html::a('Poling BEM', ['/bem/pollingbem'], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Album BEM', ['/album-bem/index'], ['class' => 'btn btn-primary']) ?>
    <h2><?= Html::encode($this->title) ?></h2>
    <hr/>
    <?php // echo $this->render('_search', ['model' => $searchModel]);  ?>

    <p>
        <?php
        if (Yii::$app->user->can('sekretarisBem')) {
            echo Html::button('Create Album Bem', ['value' => yii\helpers\Url::to(['create']), 'class' => 'btn btn-success', 'id' => 'modalButton']);
            Modal::begin([
                'id' => 'modal',
                'size' => 'modal-lg',
            ]);
            echo "<div id='modalContent'></div>";
            Modal::end();
        }
        ?>
    </p>    



    <?php
    foreach ($dataProvider as $dataAlbum) {
        echo $dataAlbum->judul . "<br>";
        echo $dataAlbum->kegiatan . "<br>";
        ?> 
        <div class="row">

            <?php

            if ($dataAlbum->foto1 != NULL) {
                ?> 
                <div class="col-lg-3 col-md-4 col-xs-6"> 
                    <a  class="thumbnail magnifier" href="<?= Yii::$app->request->baseUrl ?>/Upload/imagesBemAlbum/foto/<?= $dataAlbum->foto1; ?>">
                        <img src="<?= Yii::$app->request->baseUrl ?>/Upload/imagesBemAlbum/foto/<?= $dataAlbum->foto1; ?>" width="150px" height="200px">
                    </a>
                </div>
                <?php
            }
            if ($dataAlbum->foto2 != NULL) {
                ?>
                <div class="col-lg-3 col-md-4 col-xs-6"> 
                    <a class="thumbnail" href="#">
                        <img src="<?= Yii::$app->request->baseUrl ?>/Upload/imagesBemAlbum/foto/<?= $dataAlbum->foto2; ?>" width="150px" height="200px">
                    </a>
                </div>       
                <?php
            }
            if ($dataAlbum->foto3 != NULL) {
                ?>
                <div class="col-lg-3 col-md-4 col-xs-6"> 
                    <a class="thumbnail" href="#">
                        <img src="<?= Yii::$app->request->baseUrl ?>/Upload/imagesBemAlbum/foto/<?= $dataAlbum->foto3; ?>" width="150px" height="200px">
                    </a>
                </div>
                <?php
            }
            if ($dataAlbum->foto4 != NULL) {
                ?> 
                <div class="col-lg-3 col-md-4 col-xs-6"> 
                    <a class="thumbnail" href="#">
                        <img src="<?= Yii::$app->request->baseUrl ?>/Upload/imagesBemAlbum/foto/<?= $dataAlbum->foto4; ?>" width="150px" height="200px">
                    </a>
                </div>
                <?php
            }
            if ($dataAlbum->foto5 != NULL) {
                ?>    
                <div class="col-lg-3 col-md-4 col-xs-6"> 
                    <a class="thumbnail" href="#">    
                        <img src="<?= Yii::$app->request->baseUrl ?>/Upload/imagesBemAlbum/foto/<?= $dataAlbum->foto5; ?>" width="150px" height="200px">
                    </a>
                </div>
                <?php
            }
            ?>
            <br><br>     
        </div>
        <?php
        if (Yii::$app->user->can('sekretarisBem')) {
            echo Html::a('Update', ['update', 'id' => $dataAlbum->id_album_bem], ['class' => 'btn btn-primary']);
            echo Html::a('Delete', ['delete', 'id' => $dataAlbum->id_album_bem], [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => 'Are you sure you want to delete this item?',
                    'method' => 'post',
                ],
            ]);
        }
        ?>
        <?php
    }
    ?>
<!-- jQuery -->
    <script src="js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>
</div>

谁能帮我解决这个问题。如果您需要更多代码,请直接说。

【问题讨论】:

    标签: javascript php jquery yii


    【解决方案1】:

    如果你想插入jquery代码来查看文件,使用registeJS方法http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJs()-detail 使用 AssetBundle 注册脚本文件也是一个好习惯。 从视图中删除 <script src="js/jquery.js"></script> <script src="js/bootstrap.min.js"></script> 并使用 \yii\web\JqueryAsset::register($this); \yii\bootstrap\BootstrapPluginAsset::register($this);在您的视图顶部

    【讨论】:

    • 请详细说明您的答案并为您的建议提供解释。
    • @Evgenii 我已经在我的视图顶部添加了这段代码 $script = 1024) { jQuery("#content .row").preloader(); } jQuery('.magnifier').touchTouch(); jQuery('.spinner').animate ({'opacity':0},1000,'easeOutCubic',function (){jQuery(this).css('display','none')}); }); JS; \yii\web\JqueryAsset::register($script); \yii\bootstrap\BootstrapPluginAsset::register($script);但我又遇到了一个错误:PHP Notice – yii\base\ErrorException Undefined variable: x 有什么建议吗??
    • $x 将被 PHP $x 中的变量替换。可能你没有它所以只是逃避它 \$x
    猜你喜欢
    • 1970-01-01
    • 2017-04-04
    • 2017-06-26
    • 2016-03-15
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多