twitter-bootstrap Yii:如何在标签内制作带有 html 标签的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14570651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 16:12:42  来源:igfitidea点击:

Yii : How to make a button with an html tag inside the label

phptwitter-bootstrapyii

提问by Ricardo Fiorani

I'm trying to use some bootstrap features like the Icon glyphs in the Yii CHtml class, here is my code:

我正在尝试使用一些引导程序功能,例如 Yii CHtml 类中的图标字形,这是我的代码:

<?php 
   echo CHtml::submitButton('<i class="icon-user"></i> Login', 
       array(
           'class' => 'btn btn-large pull-right'
       )); 
 ?>

But it kinda don't "recognize" the tag and just renders the tag out like the image bellow .enter image description here

但它有点不“识别”标签,只是像下面的图像一样呈现标签。在此处输入图片说明

does anyone knows how to workaround it (without typing the html tags itself).

有谁知道如何解决它(不输入 html 标签本身)。

Thank you guys.

感谢你们。

回答by Jon

CHtml::submitButtonproduces an <input type="submit">that cannot accept additional HTML as its content. However, you can do things to taste with CHtml::tag:

CHtml::submitButton产生一个<input type="submit">不能接受额外的 HTML 作为其内容的。但是,您可以做一些事情来品尝CHtml::tag

echo CHtml::tag('button',
                array('class' => 'btn btn-large pull-right'),
                '<i class="icon-user"></i> Login');

This will produce a <button>tag that can take arbitrary HTML as its content.

这将生成一个<button>可以将任意 HTML 作为其内容的标签。

Update:As frostyterrier points out in the comments, there's a built-in method CHtml::htmlButtonthat allows you to do this even more easily:

更新:正如frostyterrier 在评论中指出的那样,有一种内置方法CHtml::htmlButton可以让您更轻松地执行此操作:

echo CHtml::htmlButton('<i class="icon-user"></i> Login',
                       array('class' => 'btn btn-large pull-right'));

回答by SJousse

Try to set 'encode' to false in htmlOptions parameter.

尝试在 htmlOptions 参数中将 'encode' 设置为 false。

<?php
    echo CHtml::submitButton('<i class="icon-user"></i> Login',
        array(
            'encode' => false,
            'class' => 'btn btn-large pull-right'
        ));
?>

回答by lafor

CHTML::submitbuttongenerates <intput type="submit" />tag and you're trying to cram HTML into its valueattribute.

CHTML::submitbutton生成<intput type="submit" />标记,而您正试图将 HTML 塞入其value属性中。

Why don't you simply add the icon-userCSS class to the button itself?

为什么不简单地将icon-userCSS 类添加到按钮本身?

echo CHtml::submitButton('Login', array(
   'class' => 'btn btn-large pull-right icon-user'
)); 

If it's a Bootstrap-defined class, it should style the input button nicely.

如果它是 Bootstrap 定义的类,它应该很好地设置输入按钮的样式。

回答by andsigno82

try this

试试这个

$this->widget("bootstrap.widget.TbButton", array(
'buttonType'=>'submit',
'label=>'Login',
'type'=>'success', //or default or warning... as you like
'icon'=>'user',
'size'=>'large', //small, mini, or just comment line for default

));

documentation: http://www.cniska.net/yii-bootstrap/#tbButton

文档:http: //www.cniska.net/yii-bootstrap/#tbButton

回答by Mahmut Ayd?n

Firstly you should change from subbmitButton to htmlButton,for example write the following code:

首先你要把subbmitButton改成htmlButton,比如写如下代码:

<?php echo CHtml::htmlButton('<span>Log in!</span>',array('type'=>'submit','class'=>'tbutton small pad')); ?>