简体   繁体   English

防止注入的标记受到父CSS的影响

[英]Prevent injected tags from being affected by parent CSS

I want to inject some HTML to another HTML page dynamically like this : 我想动态地将一些HTML注入另一个HTML页面,如下所示:

<div id="jwplayer-player"> ... </div>

In the parent HTML page, there is CSS rules for DIV as generic (This is sample , may be different according to the page) : 在父HTML页面中,DIV的CSS规则是通用的(这是示例,根据页面可能有所不同):

div {
    margin-top: 15px;
    line-height: 200%;
    white-space: normal;
}

How do I execlude my injected DIVs from being affected by them. 如何排除我注射的DIV受其影响。

The player is injected as follows : 玩家注入如下:

<div id ="vid-player-container" class="player-container"></div> 
<script type="text/javascript"> 
document.write('<script type="text/javascript" src="some.site.com/jwplayer/jwplayer.js" ></script>'); 
document.write('<script type="text/javascript" src="some.script.com/player_style1.js"></script>'); 
</script> 

If I try it alone outside those parent pages, works perfectly. 如果我在这些父页面之外单独尝试,那就完美无缺。 So the key is isolating the injected code from being affected by the parent site, note that iFrame is not an option for me. 所以关键是隔离注入的代码不受父站点的影响,请注意iFrame不是我的选项。

Rather than trying to prevent the parent page from setting CSS styles, you should assume that any/all CSS styles that can be set by the parent page will be set. 您应该假设可以设置父页面可以设置的任何/所有CSS样式,而不是试图阻止父页面设置CSS样式。 Then, take the approach of specifying your own styles with a more specific selector. 然后,采用使用更具体的选择器指定自己的样式的方法。 In particular, you can use the !important indicator in combination with and id selector to have a reasonably good chance of having your style definitions win out. 特别是,您可以将!important指示符与id选择器结合使用,以便让您的样式定义获胜。 For example: 例如:

<style type="text/css">
#jwplayer-player {
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    font-size: 100% !important;
    font: inherit !important;
    vertical-align: baseline !important;
    line-height: 1 !important;
}
</style>
<div id="jwplayer-player"> ... </div>

Note that I borrowed a good set of reasonable defaults from Eric Meyer's Reset CSS 请注意,我从Eric Meyer的Reset CSS中借用了一组合理的默认值

I think you could use the ShadowDOM to achieve what you are trying to do. 我认为你可以使用ShadowDOM来实现你想要做的事情。 https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM

It is designed to allow you to separate your components in this way, and provides isolation of CSS and JavaScript for the encapsulated DOM. 它旨在允许您以这种方式分离组件,并为封装的DOM提供CSS和JavaScript的隔离。

var shadow = document.querySelector('#container').attachShadow({mode: 'open'});
shadow.innerHTML += '<p>I am text in the injected code</p>'

For example: https://jsfiddle.net/rn46rfgx/ 例如: https//jsfiddle.net/rn46rfgx/

If you inspect element on your page, you should be able to see the isolation: 如果检查页面上的元素,您应该能够看到隔离:

在此输入图像描述

Disclaimer : Whilst this probably works for you, I'm not sure if it is the correct way to do this or if it's a bit of a "hack" - I hope others will comment on this answer with their thoughts on this! 免责声明 :虽然这可能适合你,但我不确定这是否是正确的方法,或者它是否有点“黑客” - 我希望其他人会对这个问题发表评论。

Also, I'm not sure if the browser support will be adequate for you, you can check here: http://caniuse.com/#feat=shadowdom 此外,我不确定浏览器支持是否适合您,您可以在此处查看: http//caniuse.com/#feat=shadowdom

Try 尝试

#jwplayer-player {
    margin-top: initial;
    line-height: initial;
    white-space: initial;
}

It will change all those rules to its initial values - default. 它会将所有规则更改为其初始值 - 默认值。

I am not sure if it will work in this case. 我不确定它是否适用于这种情况。

Try using the not CSS selector, update your CSS rule to become like this: 尝试使用not CSS选择器,将CSS规则更新为:

.MainContentdiv .BodyDiv :not(#jwplayer-player) div {
    margin-top: 15px;
    line-height: 200%;
    white-space: normal;
}

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

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