简体   繁体   English

CSS Force span不具有其父样式

[英]CSS Force span not to have its parent style

I have a span including multiple spans: 我的跨度包括多个跨度:

<span id="sp9">
   <span class="comment">/*
     <span class="number">  11     </span>* some text
     <span class="number">  12     </span>* -------------------
     <span class="number">  13     </span>* other text
     <span class="number">  14     </span>* some stuff
     <span class="number">  15     </span>*/
   </span>
</span>

In JavaScript code I am doing this: 在JavaScript代码中,我这样做:

function highlight(id){
    document.getElementById(id).style.backgroundColor = "gray";
}

In the CSS file I have the style of the class number like this: 在CSS文件中,我具有类number的样式,如下所示:

.number {
    color: DarkCyan;
    background-color: none !important;
    -webkit-user-select: none; 
    -moz-user-select: none; 
    -khtml-user-select: none; 
    -ms-user-select: none; 
}

When the JavaScript function is executed, the numbers are included in the background color change which I don't want to happen. 执行JavaScript函数时, numbers会包含在我不想发生的背景颜色更改中。

I tried logging the background color of all numbers and it was empty (no background color) so I know they don't have it. 我尝试记录所有numbers的背景色,但它是空的(没有背景色),所以我知道他们没有。

How can I force the numbers not to have any background color? 如何强制numbers不具有任何背景色?

I am new to these types of stuff so excuse me. 我对这些类型的东西不熟悉,所以请原谅。 I have looked at other questions here and I believe I might need to change a major thing in here. 我在这里查看了其他问题,我相信我可能需要在这里更改一个主要问题。

I have tried using div instead of span which worked but in the display the div s were on separate lines. 我尝试使用div代替有效的span ,但是在显示中div放在单独的行上。 I changed their display to inline-block and got them to be in the same line, but the highlighting started behaving just like the spans. 我将它们的display更改为inline-block并使它们处于同一行,但是突出显示开始像跨度一样。

Any ideas? 有任何想法吗?

You can follow one of the 2 approaches 您可以遵循以下两种方法之一

  • Specify a default background color for the number class ( As you are setting it to transparent by setting it to none ) 指定number类别的默认背景色(将其设置为none ,将其设置为透明)
  • Enclose the pre and post text into a span and specify a background color to them instead of the parent. 将前文本和后文本封闭在一个范围内,并为其指定背景颜色而不是父颜色。

#1 #1

 function highlight(id){ document.getElementById(id).style.backgroundColor = "gray"; } highlight("sp9"); 
 .number { color: DarkCyan; background: white; -webkit-user-select: none; -moz-user-select: none; -khtml-user-select: none; -ms-user-select: none; } 
 <span id="sp9"> <span class="comment">/* <span class="number"> 11 </span>* some text <span class="number"> 12 </span>* ------------------- <span class="number"> 13 </span>* other text <span class="number"> 14 </span>* some stuff <span class="number"> 15 </span>*/ </span> </span> 

#2 #2

 .number { color: DarkCyan; -webkit-user-select: none; -moz-user-select: none; -khtml-user-select: none; -ms-user-select: none; } .post-text { background: gray; } 
 <span id="sp9"> <span class="comment"><span class="post-text">/*</span> <span class="number"> 11 </span><span class="post-text">* some text</span> <span class="number"> 12 </span><span class="post-text">* -------------------</span> <span class="number"> 13 </span><span class="post-text">* other text</span> <span class="number"> 14 </span><span class="post-text">* some stuff</span> <span class="number"> 15 </span><span class="post-text">*/</span> </span> </span> 

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

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