javascript - 在输出中编辑 JS 和 CSS
问题描述
这是一个有趣的技巧,如何在输出中查看和编辑 CSS 和 JS。例如,在输出中,您可以将颜色从 更改red
为blue
。
我的问题是为什么我不能以同样的方式更改脚本打印的文本?我的意思是,无论进行任何编辑,打印的文本仍然是foo
而不是 eg bar
。有没有办法让它工作?
<div></div>
<p>The contents of the <code><style></code> tag:</p>
<style contenteditable>
div {
color: red; /* You can change the color right in the **output**! */
}
script {
background: lightgray;
}
style {
background: aliceblue;
}
script, style {
display: inline-block;
font-family: monospace;
white-space: pre-wrap;
width: 100%;
}
</style>
<p>The contents of the <code><script></code> tag:</p>
<script contenteditable>
function timer() {
/* You cannot do the same trick to replace foo with bar... */
document.querySelector('div').append('foo ');
}
setInterval(timer, 1000);
</script>
解决方案
因为script
标签在插入 DOM 时只执行一次,所以您只是更改它的文本内容,不会重新评估script
. 您可以模拟脚本评估,但script
这里的标签没有多大意义:
<div></div>
<p>The contents of the <code><style></code> tag:</p>
<style contenteditable>
div {
color: red; /* You can change the color right in the **output**! */
}
script {
background: lightgray;
}
style {
background: aliceblue;
}
script, style {
display: inline-block;
font-family: monospace;
white-space: pre-wrap;
width: 100%;
}
</style>
<p>The contents of the <code><script></code> tag:</p>
<script id="code" contenteditable>
document.querySelector('div').append('foo ');
</script>
<script>setInterval(() => eval(code.textContent), 1000)</script>
推荐阅读
- c# - 如何将手机连接到 Rest Api,以便我可以通过它发送短信?
- python - 如何使用 numpy 对泊松回归进行最大似然估计(MLE)
- typescript - 带有 TypeScript 的 Cypress 代码覆盖率插件
- visual-studio-code - 在 VScode 中导入测试模块时出现导入错误
- sql - SQL Server 2016:如何获取单行视图
- amazon-web-services - 无法再使用 React native 将图像上传到 Amazon S3 存储桶
- reactjs - 如何在 KeyboardDatePicker 中显示自定义文本(占位符)
- python - 将嵌套的 Jsonl 文件转换为 CSV 格式:取消嵌套 Jsonl 并提取为 CSV
- python - 使用 Python 文件在 Robot Framework 中将变量定义为变量
- java - 如何在 Java 中使用 double[] 参数填充 setter 方法?