通过 jQuery 添加内联 CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29893578/
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-08-26 16:39:13  来源:igfitidea点击:

Add inline CSS via jQuery

jqueryhtmlcssdom

提问by Mapsism Borja

I don't know jQuery very well so I don't know how to add CSS inline. This is my code:

我不太了解 jQuery,所以我不知道如何内联添加 CSS。这是我的代码:

<div class="gm-style-iw" style="top: 9px; position: absolute; left: 15px; width: 23px;">
 <div style="display: inline-block; overflow: hidden; max-height: 330px; max-width: 176px;">
  <div style="overflow: auto">
   <div id="iw-container">
   </div>
  </div>
 </div>
</div>

I want to change the max-widthof second div and add overflow: noneto the div above #iw-container.

我想更改max-width第二个 div 并添加overflow: none到上面的 div 中#iw-container

I tried this but it did not work:

我试过这个,但没有用:

var iwOuter = $('.gm-style-iw');
iwOuter.children().css({max-width:'250px !important'});
iwOuter.children().children().css({overflow:'none'});

EDITi want to customize infowindow Google and for me is important to change the value of max-width and overflow.... i think that your code maybe are good, but i don't understand why doesn't work...

编辑我想自定义信息窗口谷歌,对我来说改变最大宽度和溢出的值很重要....我认为你的代码可能很好,但我不明白为什么不起作用......

回答by Albzi

In jQuery you can do something like this:

在 jQuery 中,您可以执行以下操作:

$('.gm-style-iw > div').css('max-width', "250px !important");
$('#iw-container').parent().css('overflow', 'none');//css wasn't valid here

Using > divmeans it gets the first div only, if you want all divs to have a max-width, use:

Using> div意味着它只获取第一个 div,如果您希望所有divs 都有一个max-width,请使用:

$('.gm-style-iw').children().css('max-width', "250px !important");

Using the .parent()function means that it gets only the parent of the selected element. It's cleaner and easier to read and shorter to write.

使用该.parent()函数意味着它只获取所选元素的父元素。它更干净,更容易阅读,写起来更短。

回答by captainsac

$(".gm-style-iw div:first-child").css( "max-width", "100px");   
$("#iw-container").parent().css("overflow","none");