[英]I receive raw HTML from my database and render it on the front-end, however, one of the HTML classes does nothing even though it's applied
Here's the issue, I get some messages from the database and send them to my front-end in order to render them.这是问题所在,我从数据库中获取了一些消息并将它们发送到我的前端以呈现它们。 The message looks like this:
消息如下所示:
<span class="emoji">
<img class='responsive-image' src="https://media.discordapp.net/attachments/733747075455647898/733753424008577024/Akali.png">
</span>
So I use the v-html directive to render it like this:所以我使用 v-html 指令来渲染它:
<p v-html="message.message"></p>
I also have the 2 classes in the same component:我在同一个组件中也有 2 个类:
.emoji {
display: inline-block;
width: 30px;
height: 30px;
}
.responsive-image {
width: 100%;
height: 100%;
}
The problem is that even though the <span>
element is rendered with a class of .emoji
, the rules of that class are not applied.问题是,即使
<span>
元素使用 .emoji 的.emoji
呈现,该 class 的规则也不会应用。 Also when I inspect the <span>
element, on the right side of the developers console, I don't see the <span>
class or its rules.此外,当我检查
<span>
元素时,在开发人员控制台的右侧,我看不到<span>
class 或其规则。 The weird thing is that the responsive-image
class on the image works just fine.奇怪的是,图像上的
responsive-image
class 工作得很好。
Surprisingly, if I hard-code the HTML like this:令人惊讶的是,如果我像这样对 HTML 进行硬编码:
<div class='message-container'>
<div class="username-date-container">
<p class='username'>Username</p>
<p class='date'>Date</p>
</div>
<p>
<span class="emoji">
<img class='responsive-image' src="https://media.discordapp.net/attachments/733747075455647898/733753424008577024/Akali.png">
</span>
</p>
</div>
Everything gets rendered correctly and the rules of the emoji class are applied.一切都正确呈现,并应用了表情符号 class 的规则。 So I've no clue what's going on.
所以我不知道发生了什么。
The problem is using scoped CSS with v-html
.问题是使用范围 CSS 和
v-html
。 Any markup injected via v-html
is treated like being from a child component.通过
v-html
注入的任何标记都被视为来自子组件。 From the docs :从文档:
DOM content created with
v-html
are not affected by scoped styles, but you can still style them using deep selectors.使用
v-html
创建的 DOM 内容不受作用域 styles 的影响,但您仍然可以使用深度选择器设置它们的样式。
If you inspect the elements in the browser developer tools (as in your picture) you'll see elements with attributes like data-v-2bc3d388
, where the last part can vary.如果您检查浏览器开发人员工具中的元素(如您的图片中所示),您将看到具有
data-v-2bc3d388
等属性的元素,其中最后一部分可能会有所不同。 This attribute will be applied to every element that is created by the template for that particular component.该属性将应用于模板为该特定组件创建的每个元素。 However, it won't be present for elements inserted using
v-html
or those created by child components.但是,它不会出现在使用
v-html
插入的元素或由子组件创建的元素中。 As a result, any scoped CSS won't apply to those elements.因此,任何范围的 CSS 都不适用于这些元素。
You can see why by inspecting the output CSS.您可以通过检查 output CSS 来了解原因。 In your picture, if you look over on the right-hand side you'll see some rules for
.message-container[data-v-2bc3d388]
.在您的图片中,如果您查看右侧,您会看到
.message-container[data-v-2bc3d388]
一些规则。 To the right of that, written in grey is the source of the rules, in this case <style>
.在其右侧,用灰色书写的是规则的来源,在本例中为
<style>
。 If you click on that it'll jump to the relevant CSS in the page and you can see the output CSS for yourself.如果您点击它,它将跳转到页面中的相关 CSS,您可以自己查看 output CSS。 You should see that all the selectors require elements to have
[data-v-2bc3d388]
, which your v-html
elements do not.您应该看到所有选择器都要求元素具有
[data-v-2bc3d388]
,而您的v-html
元素则没有。
As for why your responsive
class is working correctly, that's not entirely clear.至于为什么您的
responsive
式 class 工作正常,这并不完全清楚。 My best guess would be that you've got that same rule defined in another file but you've forgotten to scope it so it has leaked out and is effectively global.我最好的猜测是您在另一个文件中定义了相同的规则,但是您忘记了 scope 它因此它已经泄漏并且实际上是全局的。 Inspecting the relevant element should allow you to check.
检查相关元素应该允许您检查。 You could also try removing the CSS from the current component to see whether it makes any difference.
您还可以尝试从当前组件中删除 CSS 以查看它是否有任何区别。
There are various ways you might solve this.有多种方法可以解决这个问题。 You could make that particular rule unscoped, either by adding it to your global CSS or by adding another
<style>
tag to your component that isn't scoped.您可以通过将特定规则添加到全局 CSS 或将另一个
<style>
标记添加到未限定范围的组件来使该特定规则成为无范围的。
Another solution would be to use a deep selector, same as you would if you wanted to style a child component:另一种解决方案是使用深度选择器,就像你想为子组件设置样式一样:
https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors
I assume this is more like a CSS problem where there exists a CSS selector has a higher score to override your span.emoji class, so try to inspect it again to see which part of your CSS code has higher select specificity or simply add ".important" to your,emoji styling query. I assume this is more like a CSS problem where there exists a CSS selector has a higher score to override your span.emoji class, so try to inspect it again to see which part of your CSS code has higher select specificity or simply add ".对您的表情符号样式查询很重要”。 only if the CSS file is scoped to that particular HTML so it won't affect your other components' styling properties.
仅当 CSS 文件的范围限定为该特定 HTML 时,它才不会影响其他组件的样式属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.