简体   繁体   English

MaterializeCSS - 更改 select 选项字体系列

[英]MaterializeCSS - Change select option font family

I have a select element which looks like this:我有一个 select 元素,如下所示:

 <div class="input-field container"> <select> <option selected>Roboto</option> <option>Kalam</option> <option>Karma</option> <option>Montserrat</option> </select> </div>

Now, for every one of them, I want to change the font family to what the actual inner html is, to give a preview of the font you're about to select.现在,对于它们中的每一个,我想将字体系列更改为实际的内部 html,以预览您将要使用的字体 select。

But putting styles directly on the option doesn't work for me.但是将 styles 直接放在选项上对我不起作用。

I found a solution targeting all options from css, however, that wouldn't enable me to achieve the wanted behaviour.我找到了一个针对 css 的所有选项的解决方案,但是,这无法让我实现想要的行为。

Any suggestions?有什么建议么?

Thanks!谢谢!

I tried with CSS, but didn't seem to work. 我尝试使用CSS,但似乎没有用。

You can use javascript to set the style same as the option 's value with the style attribute: 您可以使用javascript通过style属性将样式设置为与option的值相同:

 document.querySelectorAll(".selectFont option").forEach(function(el){ el.style.fontFamily = el.value; }) 
 <div class="input-field container"> <select class="selectFont"> <option value="monospace" selected>monospace</option> <option value="Arial" >Arial</option> <option value="sans-serif" >sans-serif</option> </select> </div> 

This is quite old, but I had the exact same problem - I also wanted to change the font family for text in materialize select options, but after much css manipulation I could not get it to work.这已经很老了,但是我遇到了完全相同的问题-我还想更改具体化 select 选项中的文本字体系列,但是经过多次 css 操作后,我无法使其正常工作。

I finally found this answer by wahmal - if you add the class 'browser-default' to your select element, it will remove the materialize styling.我终于通过 wahmal 找到了这个答案- 如果将 class 'browser-default' 添加到 select 元素中,它将删除具体化样式。 You can then change the font family of the options.然后,您可以更改选项的字体系列。 (That said, it won't look like a materialize element any longer if you do this.) Example: (也就是说,如果你这样做,它看起来就不再像物化元素了。)示例:

  <div>
    <select class="browser-default" id="font-dropdown">
       <option selected style="font-family: 'courier';">courier</option>
       <option style="font-family: 'arial;'">arial</option>
       <option style="font-family: 'Verdana;'">Verdana</option>
    </select>
    <label>Font</label>
  </div>

I did not need.important to get it to work.我不需要.important 让它工作。 Hope this helps someone.希望这可以帮助某人。

https://materializecss.com/select.html https://materializecss.com/select.html

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

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