首页 \ 问答 \ Bootstrap同时切换按钮图标和文本(Bootstrap toggle button icon and text at the same time)

Bootstrap同时切换按钮图标和文本(Bootstrap toggle button icon and text at the same time)

我正在用Jquery使用Bootstrap 3 。 我有一个在span标签中包含图标的按钮(使用引导程序的glypicon)

<button id="swapArchived" class="btn btn-default btn-sm showArchived">
       <span class="glyphicon glyphicon-road"></span> Show Archived
</button>

我想单击时更改图标和文字。 有没有更好的方法,除了,

$('#swapArchived').on('click', function () {
    var $el = $(this);
    if($el.hasClass('showArchived'))
    {
      $el.html('<span class="glyphicon glyphicon-fire"></span> Show Incomplete');
    }
    else
    {      
      $el.html('<span class="glyphicon glyphicon-road"></span> Show Archived');
    }
      $el.toggleClass('showArchived');
  });

我很好奇我是否可以同时切换按钮文本和span类。 尽量避免每次点击都要写满量程。

谢谢-


I am using Bootstrap 3 with Jquery. I have a button that contains an icon in a span tag.(using glypicon of bootstrap)

<button id="swapArchived" class="btn btn-default btn-sm showArchived">
       <span class="glyphicon glyphicon-road"></span> Show Archived
</button>

I want to change the icon and text when clicked. Is there any better way other than,

$('#swapArchived').on('click', function () {
    var $el = $(this);
    if($el.hasClass('showArchived'))
    {
      $el.html('<span class="glyphicon glyphicon-fire"></span> Show Incomplete');
    }
    else
    {      
      $el.html('<span class="glyphicon glyphicon-road"></span> Show Archived');
    }
      $el.toggleClass('showArchived');
  });

I was curious whether I can toggle button text and span class at the same time. Trying to avoid writing span on every click.

Thanks-


原文:https://stackoverflow.com/questions/19898599
更新时间:2022-02-28 11:02

最满意答案

您可以使用包含两个单元格的表格。 将input字段放在左侧单元格中,将图像放在右侧单元格中。 喜欢这个:

<input type="text" style="width:95%" /><br /><br />
<input type="text" style="width:95%" /><br /><br />
<table>
    <tr>
        <td><input type="text" style="width:95%" />
            <br />
            <br />
            <input type="text" style="width:95%" />
        </td>
        <td><img src="http://icons.iconarchive.com/icons/martin-berube/people/256/baby-icon.png" /></td>
    </tr>
</table>
<br />
<textarea style="width:95%"></textarea>


You could use a table with two cells. Put the input fields in the left cell, and the image in the right cell. Like this:

<input type="text" style="width:95%" /><br /><br />
<input type="text" style="width:95%" /><br /><br />
<table>
    <tr>
        <td><input type="text" style="width:95%" />
            <br />
            <br />
            <input type="text" style="width:95%" />
        </td>
        <td><img src="http://icons.iconarchive.com/icons/martin-berube/people/256/baby-icon.png" /></td>
    </tr>
</table>
<br />
<textarea style="width:95%"></textarea>

相关问答

更多