简体   繁体   English

在动态生成的元素中覆盖CSS

[英]Overriding css in dynamically generated element

I'm using bootstrap-select , which adds div on top of select . 我正在使用bootstrap-select ,它将div添加到select之上。 In other terms, I define: 换句话说,我定义:

<select id="@Html.IdForModel()" 
    name="@Html.NameForModel()" 
    class="selectpicker form-control" 
    data-live-search="true"></select>
...

 var selectBox = $('#@Html.IdForModel()');
 selectBox.selectpicker()

But in the page with Developer Tools I see: 但是在带有开发人员工具的页面中,我看到了:

<div class="btn-group bootstrap-select form-control">
  <button .../>
  <div class="dropdown-menu open" .../>
  <select .../>
</div>

Now I need this outer div to be "display:block" , this fixes some layout problems I experience. 现在,我需要这个外部div为"display:block" ,这可以解决我遇到的一些布局问题。 But btn-group and bootstrap-select both apply "display:block-inline" . 但是btn-groupbootstrap-select都适用"display:block-inline"

I've tried: 我试过了:

  • Defining my own style for bootstrap-select in separate css file 在单独的CSS文件中为我的bootstrap-select定义自己的样式
  • Adding !important to my definition 在我的定义中添加!important
  • selectBox.parent().addClass("select-parent-div"); , with my css having: .select-parent-div{display: block !important} ,而我的CSS具有: .select-parent-div{display: block !important}

The only thing that has worked is selectBox.parent().css("display", "block"); 唯一起作用的是selectBox.parent().css("display", "block"); but I would really like to have my styles in css files. 但我真的很想在CSS文件中添加样式。 Why doesn't css apply and what can I do about that? 为什么CSS不适用,我该怎么办?

You need to check if the style that apply bootstrap is marked as !important in this case css will not take your style in consideration. 您需要检查是否将应用引导程序的样式标记为!important,在这种情况下,css不会考虑您的样式。

I faced this problem before and the trick that is work is to create the same style with the same name of bootstrap in your page and change the properties as you like. 我之前遇到过这个问题,有效的窍门是在页面中创建具有相同引导名称的相同样式,并根据需要更改属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM