HTML::a 中的目标 _blank 不工作 yii2

target _blank in HTML::a not working yii2

我正在使用下面的代码。

[
            'attribute' => 'Application',
            'format' => 'html',
            'value' => function ($dataProvider) {
                $student_username = $dataProvider->student_username;
                return Html::a('Print', Url::toRoute(['/coordinatorpanel/print-form', 'student_username' => $student_username]),
                    ['target' => '_blank', 'class' => 'btn btn-success center-block']);
            },
        ]

HTML 输出:

<a class="btn btn-success center-block" href="/nse/frontend/web/index.php?r=coordinatorpanel%2Fprint-form&amp;student_username=COR39690113" target="_blank">

但是,当我单击 link 时,我没有导航到新选项卡,请求是在同一个选项卡中处理的。我在 'Mozilla' 和 'Chrome' 上都试过了。

任何帮助将不胜感激:)

我假设这段代码在一些格式化输出的小部件中,因为这是常见的情况。如果是这样,只需将格式更改为 raw.

我不得不另辟蹊径才能正确 运行 编码。

You can click here for reference

下面是我修改并为我工作的一段代码。我必须设置 'onclick' 事件并将 href 设置为空。

[
    'attribute' => 'Application',
    'format' => 'raw',
    'value' => function ($dataProvider) {
        $student_username = $dataProvider->student_username;
        return Html::a('Print', '',
            ['onclick' => "window.open ('".Url::toRoute(['/coordinatorpanel/print-form', 
                          'student_username' => $student_username])."'); return false", 
             'class' => 'btn btn-success center-block']);
    },
],

在 gridview 中禁用 Pjax。在我的例子中,我想在新的 window 中显示图像,但它在网格中显示为乱码。

'format' => "raw",
'value' => function ($d) {
     return Html::a($d->image_filename, '/'.$d->imagePath(),['target'=>'_blank', 'data-pjax'=>"0"] );
    },

ps。注意使用原始格式的 XSS

尝试像下面这样更改您的代码

[
        'attribute' => 'Application',
        'format' => 'raw',
        'value' => function ($dataProvider) {
            $student_username = $dataProvider->student_username;
            return Html::a('Print', Url::toRoute(['/coordinatorpanel/print-form', 'student_username' => $student_username]),
                ['target' => '_blank', 'class' => 'btn btn-success center-block']);
        },
    ]

这里使用了'format' => 'raw'来避免格式​​化。

试试这个方法,效果也很好-

问题也可能在pjax。即使您使用 row 格式。

在这种情况下只需添加 linkSelector 参数:

\yii\widgets\Pjax::begin(
    ['id' => 'samle', 'linkSelector' => 'a:not(.target-blank)']
);

并在您的链接中添加相应的 css class:

return Html::a(
    'Print',
    ['/site/print', 'id' => $model->id],
    ['target'=>'_blank', 'class' => 'target-blank']
);

这将仅阻止这些链接 pjax,以便它们可以在新标签页中打开。