【问题标题】:console.log output to document instead of consoleconsole.log 输出到文档而不是控制台
【发布时间】:2021-12-07 18:48:34
【问题描述】:

我有一个非常简单的 html 文件,它只包含以下内容:

<html>
    <head>
        <script defer src="./bundle_ACTUAL.js"></script>
    </head>
</html>

./bundle_ACTUAL.js 运行时,我会在 Chrome 控制台中获得输出

有没有办法将./bundle_ACTUAL.js 的输出重定向到实际打印到文档中?

【问题讨论】:

    标签: javascript html google-chrome console


    【解决方案1】:

    您可以覆盖console.log 的默认行为,改为写入document

    console.log = function(e){ document.write(e) }
    
    console.log("Hello World!")

    或者,只需将每次出现的console.log 替换为document.write

    document.write("Hello World!")

    【讨论】:

    • 谢谢,但是我应该把这两行代码放在哪里呢?在html文件中?还是在 ./bundle_ACTUAL.js 中?
    • @user3438258 你应该把它放在./bundle_ACTUAL.js的第一行
    • 所以,我按照你的建议做了,现在页面显示“'Hello World”。问题是当我使用 "html = urlopen(req).read()" 和 python 来获取网页内容时,我没有得到 "hello world",而是 "'\r\n \r\n \r\n \r\n'"。有什么办法吗?
    • @user3438258 urlopen(req).read() 返回什么?
    • 它从字面上返回一个字符串 "b'\r\n \r\n \r\ n \r\n'" 是原始 html 文件的源代码,而不是 bundle_ACTUAL.js 的输出。
    猜你喜欢
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多