简体   繁体   English

通过innerHTML输入时,span不适用CSS样式

[英]span doesn't apply css style when entered via innerHTML

I have and Angular app, and as part of it I show query result in a div (has ID of JSONContainer). 我有一个Angular应用程序,作为它的一部分,我在div中显示了查询结果(其ID为JSONContainer)。

I wanted to highlight the specific query inside the result, so I used a pipe that searchs the results, and replaces the FIELD:VALUE in the text with: 我想突出显示结果中的特定查询,因此我使用了一个管道来搜索结果,并将文本中的FIELD:VALUE替换为:

<span class="highlightSpan">FIELD:VALUE</span>.

I added the following css to the component: 我向组件添加了以下CSS:

div#JSONContainer span.highlightSpan{
    background-color: rgba(28, 243, 28, 0.5) !important;
    color:red !important;
    padding: 1px;
    margin:1px;
}

I tried adding the same style under .highlightSpan as well, with the same results. 我也尝试在.highlightSpan下添加相同的样式,并获得相同的结果。

I insert this span and the rest of the text via innerHTML in the containing component, using the 'data' variable, that stores the entire JSON, and 'query' variable that stores the query: 我通过使用存储整个JSON的'data'变量和存储查询的'query'变量,通过innerHTML在包含组件中插入了该跨度和其余文本:

    <div class="col-xs-12" id="JSONContainer"
        [innerHTML]="data | textHighlight:query">
    </div>

(I'm not using string interpolation because the data var is json shaped which I styled using html code,like html br tag, and string interpolation shows only text). (我不使用字符串插值,因为数据变量是json形状的,我使用html代码(例如html br标签)设置了样式,而字符串插值仅显示文本。)

When I point my mouse to the relevent span in google dev tools, I see that the span with the class was created: 当我将鼠标指向google dev工具中的relevent span时,我看到创建了带有该类的span:

跨度

But the css style is not applyed, and its not even showing in google dev tools: 但是css样式未应用,甚至在Google开发工具中也未显示:

在此处输入图片说明

EDIT: I know it dosen't show (I erased it from the img), but the span is located in the div#JSONContainr, as described. 编辑:我知道它没有显示(我从img中删除了它),但是跨度位于div#JSONContainr中,如所述。

EDIT 2: Here is the relevant tree: 编辑2:这是相关的树:

在此处输入图片说明

Why is this happening? 为什么会这样呢?

How to make the css style apply? 如何使CSS样式适用?

Thanks! 谢谢!

In your PIPE you have to import 在您的PIPE中,您必须导入

import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

Add to constructor 添加到构造函数

  constructor(private sanitizer: DomSanitizer) {
  }

In transform function return 在转换函数中返回

this.sanitizer.bypassSecurityTrustHtml('YOUR HTML');

This happens because you are adding the span element dynamically and it is not part of your component, therefore the span element is not decorated with component style isolation. 发生这种情况是因为您正在动态添加span元素,它不是组件的一部分,因此span元素未使用组件样式隔离进行修饰。 When you explore your components rendered html then you see that each element has attribute like _ngcontent-c2 but your span doesn't, so it is not part of that component styling. 探索以html呈现的组件时,您会看到每个元素都具有_ngcontent-c2类的属性,但是您的跨度却没有,因此它不是该组件样式的一部分。

You can change your css to: 您可以将CSS更改为:

::ng-deep div#JSONContainer span.highlightSpan

to style descendants of your component (other components or elements added dynamically). 为组件的后代(其他动态添加的组件或元素)设置样式。 Or you can add the styling to your global style so it is not part of that component isolation. 或者,您可以将样式添加到全局样式中,这样它就不会成为组件隔离的一部分。

I have created stackblitz demo to simulate your situation. 我创建了stackblitz演示来模拟您的情况。

You can read more about angular view encapsulation . 您可以阅读有关角度视图封装的更多信息。

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

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