选择下拉列表后图标消失(并动态加载标签值)

时间:2018-03-01 23:29:40

标签: jquery html css bootstrap-4

我在HTML中为文本值添加了一个图标。问题是它正被应用于Bootstrap按钮。选择下拉列表时,它会从外部源获取值,并且按钮标签会更改。当它取这个值时,里面的文本会改变,因此不再包含图标。我尝试通过类将它添加到div中,但这会永久地更改文本类型,我不想这样做。有任何想法吗?

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="fenster">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Reise nach Jerusalem</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkBox">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="spacing">5</property>
            <property name="homogeneous">True</property>
            <child>
              <object class="GtkButton" id="links">
                <property name="label" translatable="yes">Left</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="rechts">
                <property name="label" translatable="yes">Right</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="quit">
                <property name="label" translatable="yes">Quit</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">2</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkDrawingArea" id="zeichenbrett">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

如果我这样添加,整个按钮文字会改变......

  <div class="col-2" style="padding: 20px;">
            <div class="btn btn-primary dropdown-toggle dropdown-toggle-split" id="acctFilter" data-toggle="dropdown" acctid="placeholder">
                <i class="fa fa-building"></i>&nbsp; SELECT ACCOUNT
                <span class="sr-only">Toggle Dropdown</span>
            </div>
            <div class="dropdown-menu" id="acctList" role="menu" aria-labelledby="dropdownMenu">
            </div>
        </div>

2 个答案:

答案 0 :(得分:0)

您可以创建自定义css类来解决此问题,如下面的代码段所示。

从技术上讲,您可以将一小块自定义css添加到现有类之一。但保持清洁更好。

::before伪类具有content属性,允许您指定所需的图标/内容。您可以通过在此文件中搜索图标名称来找到该作品的代码:

https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css

padding-right允许您指定图标和文字之间的间距。

点击&#34;运行代码段&#34;以下进行实时测试:

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    
<style>
.icon-building::before {
    font-family: "FontAwesome";
    content: "\f1ad";
    padding-right: 10px;
}
</style>

<div class="container">
    <div class="row">
        <div class="col-2" style="padding: 20px;">
            <div class="btn icon-building btn-primary dropdown-toggle dropdown-toggle-split" id="acctFilter" data-toggle="dropdown" acctid="placeholder">
                SELECT ACCOUNT
                <span class="sr-only">Toggle Dropdown</span>
            </div>
            <div class="dropdown-menu" id="acctList" role="menu" aria-labelledby="dropdownMenu">
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我不确定我完全理解,但我假设你有代码来处理下拉菜单的点击。在该代码中,您正在更改文本。在没有看到代码的情况下,我唯一的建议就是在一个范围中包含按钮的文本,添加一个类以便从代码中轻松选择,只需更改范围的文本即可。所以,html看起来像:

&#13;
&#13;
<div class="col-2" style="padding: 20px;">
  <div class="btn btn-primary dropdown-toggle dropdown-toggle-split" id="acctFilter" data-toggle="dropdown" acctid="placeholder">
    <i class="fa fa-building"></i><span class="my-btn-text">SELECT ACCOUNT<span>
    <span class="sr-only">Toggle Dropdown</span>
  </div>
  <div class="dropdown-menu" id="acctList" role="menu" aria-labelledby="dropdownMenu">
  </div>
</div>
&#13;
&#13;
&#13;

如果这没有帮助,请同时包含修改元素值的所有代码。