gpt4 book ai didi

javascript - 出于样式目的在内联元素中使用 encase javascript 是否是一种不好的做法?

转载 作者:太空宇宙 更新时间:2023-11-04 16:18:58 24 4
gpt4 key购买 nike

出于样式目的在内联元素中使用 javascript 中的 encase 打印文本是否是一种不好的做法?如果是这样,设置 javascript 打印文本样式的合适方法是什么?

例如:

<script>
function produceMessage(){
var msg= 'This should print';
return msg;
}
</script>


<span style="color:red">script>
document.write(produceMessage());
</script></span>

编辑:将 document.body.appendChild(document.createTextNode(produceMessage())); 更改为 document.write(produceMessage());

最佳答案

Would it be bad practice to use encase printed text from javascript in inline elements for styling purposes?

一般来说,是的。在 HTML 之上构建 JavaScript。如果在没有运行 JavaScript 的情况下文档的某些部分没有意义,则应该使用 JavaScript 添加它,而不是在基本 HTML 中。

If so, what's the appropriate way to style javascript printed text?

与文本一起生成适当的语义标记。 (当 HTML 中没有任何内容描述您要添加的内容的语义时,Span 是最后的手段)。

<span style="color:red"><script>document.body.appendChild(document.createTextNode(produceMessage()));

那无论如何都行不通。文本被附加到 <body>所以元素不会在<span>里面.

关于javascript - 出于样式目的在内联元素中使用 encase javascript 是否是一种不好的做法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6966316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com