Yii2:HTML 不会使用工具提示呈现

Yii2: HTML won't be rendered using Tooltip

提示:已插入 Pastebin 链接,如我上一条评论中所示

提示:Muhammad 的解决方案仍然无效(参见新工具提示的图片)!

我的布局文件是这样编码的:

<?php

use yii\helpers\Html;
use common\wsl_components\AdminLteAsset;

$js = <<<SCRIPT
$(function () {
   $('body').tooltip({
    selector: '[data-toggle="tooltip"]',
        html:true
    });
});
SCRIPT;
// Register tooltip/popover initialization javascript
$this->registerJs($js);
AdminLteAsset::register($this);
$this->beginPage()
?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
    <head>
        <meta charset="<?= Yii::$app->charset ?>"/>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?= Html::csrfMetaTags() ?>
        <title><?= Html::encode($this->title) ?></title>
        <?php $this->head(); ?>
    </head>
    <body class="hold-transition skin-blue sidebar-mini sidebar-collapse">
        <?php $this->beginBody(); ?>
        <div class="wrapper">
            <?=
            $this->render(
                    'header.php'
            );
            ?>
            <?=
            $this->render(
                    'left.php'
            );
            ?>
            <?=
            $this->render(
                    'content.php', ['content' => $content]
            );
            ?>
        </div>

        <?php $this->endBody(); ?>
    </body>
</html>
<?php $this->endPage(); ?>

我的GridView是这样编码的:

[
    'attribute' => $dummy ,
    'label' => Yii::t ( 'app' , 'Charakterisierung' ) ,
    'format' => 'html' ,
    'value' => function($model) {
        if ( !empty ( $model->person->personentypDominant->typ_name ))  {
            $tag = Html::tag ( 'span' , 'Tooltip-Touch Me!' , [
                   // html-tags won't be rendered in title
                   'title' => $model->person->personentypDominant->typ_empfehlung ,
                   'data-placement' => 'left' ,
                   'data-toggle'=>'tooltip',
                   'style' => 'white-space:pre;border:1px solid red;'
            ] );
            return $tag . "<br>" . $model->person->personentypDominant->typ_verhaltensmerkmal_im_team_1 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_bei_stress_3 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_am_arbeitsplatz_4;
        }
    }
],

然而,HTML-工具提示中的标签将不会呈现。它们出现在数据库中,例如像这样:

Verhaltensempfehlung:<br><ul><li>
Kompetenz und Selbstbewusstsein zeigen,</ul></li>

不知道为什么,上面的标签不会被解释。它们在工具提示中是硬编码的。任何想法,我做错了什么?

编辑:穆罕默德回答了我的问题完全一样,但回答没有解决我的问题! 为了说明我的问题,请看附件!

你显然有 2 个错误

  1. 在此 view 中,您在行 149 中提供了跨度上缺少的属性 data-toggle="tooltip"

    将它们更改为

    $tag_rot = Html::tag(
        'span', 'Typ Rot', [
            'title' => $tooltip_rot,
            'data-toggle' => 'tooltip',
            'data-placement' => 'left',
            'style' => 'white-space:pre;border:2px solid red;'
        ]
    );
    $tooltip_green = \common\modules\lookup\models\LPersonentyp::findOne(2)->typ_empfehlung;
    $tag_green = Html::tag(
        'span', 'Typ Grün', [
            'title' => $tooltip_green,
            'data-toggle' => 'tooltip',
            'data-placement' => 'left',
            'style' => 'white-space:pre;border:2px solid green;'
        ]
    );
    $tooltip_blue = \common\modules\lookup\models\LPersonentyp::findOne(3)->typ_empfehlung;
    $tag_blue = Html::tag(
        'span', 'Typ Blau', [
            'title' => $tooltip_blue,
            'data-toggle' => 'tooltip',
            'data-placement' => 'left',
            'style' => 'white-space:pre;border:2px solid blue;'
        ]
    );
    
  2. 在您的 Gridview 列中,您使用的是 "format"=>"html" 而您应该使用 "format"=>"raw"

    将列定义更改为以下内容

    [
        'attribute' => $dummy ,
        'label' => Yii::t ( 'app' , 'Charakterisierung' ) ,
        'format' => 'raw' ,
        'value' => function($model) {
            if ( !empty ( $model->person->personentypDominant->typ_name ) ) {
                $tag = Html::tag ( 'span' , 'Tooltip-Touch Me!' , [
                            'title' => $model->person->personentypDominant->typ_empfehlung ,
                            'data-placement' => 'left' ,
                            'data-toggle' => 'tooltip' ,
                            'style' => 'white-space:pre;border:1px solid red;'
                ] );
                return $tag . "<br>" . $model->person->personentypDominant->typ_verhaltensmerkmal_im_team_1 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_bei_stress_3 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_am_arbeitsplatz_4;
            }
        }
    ] ,
    

我之前给出的 更侧重于 html::tag() 的工具提示用法,我使用了你的 gridview 列代码来复制粘贴,但忘了在使用时提及它在 gridview 中,我也更新了那个答案。

如果您在@Muhammad Omer Aslam 的回答后仍然无法在工具提示中看到 HTML 视图。

  1. 尝试添加'data-html' => 'true'或用js方式$(selector).tooltip({html: true})注意:基于bootstrap官方文档Use text if you're worried about XSS attacks.
  2. 检查您是否不包含 jquery-ui 工具提示小部件。这将覆盖 bootstrap 的工具提示。 要修复它,请添加如下脚本:
$(selector).tooltip({
    content: function () {
        return this.getAttribute("title");
    },
});

In this link 你可以找到其他方法修复 jQeuer-UI tooltip

请注意,Bootstrap 4.3.1 和 3.4.1 现在带有 HTML 消毒剂,大多数标签现在默认被抑制。

这里是bootstrap blog annuncement and the docs for 3.4.1。 所以现在您需要将工具提示(或弹出窗口)中的任何其他标签列入白名单 HTML

var myDefaultWhiteList = $.fn.tooltip.Constructor.DEFAULTS.whiteList

// To allow table elements
myDefaultWhiteList.table = []

// To allow td elements and data-option attributes on td elements
myDefaultWhiteList.td = ['data-option']

// You can push your custom regex to validate your attributes.
// Be careful about your regular expressions being too lax
var myCustomRegex = /^data-my-app-[\w-]+/
myDefaultWhiteList['*'].push(myCustomRegex)

// initialize your tooltip
$(function () {
    $('[data-toggle="tooltip"]').tooltip({
        // set your custom whitelist
        whiteList: myDefaultWhiteList
    });
});