在 JavaScript 中创建 DOM 对象

<div class="comments"></div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

    const darkModeMeidaQuery = window.matchMedia('(prefers-color-scheme: dark)');


    function updateForDarkModeChange() {

        if (darkModeMeidaQuery.matches) {

            var colorScheme = 'dark'

        } else {

            var colorScheme = 'light'

        }

        $('.comments').append('<script src="https://utteranc.es/client.js" repo="user/repo-name" issue-term="pathname" theme="github-'+colorScheme+'" crossorigin="anonymous" async><\/script>');

    }


    darkModeMeidaQuery.addListener(updateForDarkModeChange);

    updateForDarkModeChange();

</script>

我正在尝试加载话语以将浏览器主题与上述脚本匹配。但是,该对象是正常创建的,但它抛出:Uncaught Error: "issue-term" or "issue-number" must be specified. 我尝试转义字符,但它不起作用。有没有我写错的部分?



梦里花落0921
浏览 170回答 1
1回答

MYYA

不错的工作!大多数新用户不包含您已经完成的最小可重现示例!如有疑问,请始终咨询问题跟踪器。你没有做错任何事,但是 jQuery 正在清除你的属性。按照问题评论中的说明进行操作:<div id="comments"></div><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script>&nbsp; &nbsp; const darkModeMeidaQuery = window.matchMedia('(prefers-color-scheme: dark)');&nbsp; &nbsp; const comments = document.getElementById('comments');&nbsp;&nbsp; &nbsp; function updateForDarkModeChange() {&nbsp; &nbsp; &nbsp; &nbsp; if (darkModeMeidaQuery.matches) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var colorScheme = 'dark'&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var colorScheme = 'light'&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var script = document.createElement('script');&nbsp; &nbsp; &nbsp; &nbsp; script.src = 'https://utteranc.es/client.js';&nbsp; &nbsp; &nbsp; &nbsp; script.setAttribute('repo', 'ergo9ine/sfdb_tracker');&nbsp; &nbsp; &nbsp; &nbsp; script.setAttribute('issue-term', 1);&nbsp; &nbsp; &nbsp; &nbsp; script.setAttribute('theme', 'github-dark');&nbsp; &nbsp; &nbsp; &nbsp; script.setAttribute('crossorigin', 'anonymous');&nbsp; &nbsp; &nbsp; &nbsp; comments.appendChild(script)&nbsp; &nbsp; }&nbsp; &nbsp; darkModeMeidaQuery.addListener(updateForDarkModeChange);&nbsp; &nbsp; updateForDarkModeChange();</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript