【发布时间】:2020-05-13 18:44:04
【问题描述】:
我有一个字符串,比如说 headData,它是 <script> 和 <style> 标记的组合。对于 Ex(带有虚拟数据) -
let headData = '<style>
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 300;
src: local('Roboto Light'), local('Roboto-Light'), url(path-to.woff) format('woff');
}</style>
<script>var isPresent = false;</script>
<script>var isContent = true;</script>
<style>@font-face {
font-family: 'Courgette';
font-style: normal;
font-weight: 400;
src: local('Courgette Regular'), local('Courgette-Regular'), url(path-to.woff2) format('woff2');}</style>'
我在标签中注入了整个 headData,如下所示。
<script dangerouslySetInnerHTML={{__html: headData}} />
我不想注入<style>标签相关数据,只想注入所有<script>标签相关数据。
所以我最终要注入的类似于 -
let headData = '<script>var isPresent = false;</script>
<script>var isContent = true;</script>'
在 Javascript 中实现这一目标的正确方法是什么?
【问题讨论】:
标签: javascript reactjs styles script-tag