Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript使用HTML标记_Javascript_Html - Fatal编程技术网

JavaScript使用HTML标记

JavaScript使用HTML标记,javascript,html,Javascript,Html,我想问一下如何使用html标记,例如JavaScript 我的代码: document.addEventListener('DOMContentLoaded', function() { if (window.location.href.indexOf("/login?from=") !== -1) { var hint=document.createTextNode("<b>Hello</b>"); var node = document.get

我想问一下如何使用html标记,例如JavaScript

我的代码:

document.addEventListener('DOMContentLoaded', function() {
  if (window.location.href.indexOf("/login?from=") !== -1) { 
    var hint=document.createTextNode("<b>Hello</b>"); 
    var node = document.getElementById("main-panel"); 
    node.insertBefore(hint, node.firstChild); 
  }
})
但是它们显示文本字符串hello,并不会使其变大

我只能在js文件中工作,我没有机会在html文件中使用div标记。首先创建一个元素b,然后将所需的文本添加到其中。 var bold=document.createElement'b' bold.innerHTML=您好 var parent=document.getElementByIdhaha; parent.appendChildbold 把课文放在这里
您可以使用以下脚本:

        <script>
        document.addEventListener('DOMContentLoaded', function() {
        if (window.location.href.indexOf("/login?from=") !== -1) { 
           var hint = '<b>Hello</b>';
           document.write(lines);
           var node = document.getElementById("main-panel"); 
           node.insertBefore(hint, node.firstChild); 
       }
       })   
       </script>

我测试了一下,它成功了

因为您将其作为文本插入。请改为尝试innerHTML或document.createElementb和AppendChild在我的示例中如何使用这一行,因为如果我将行var hint=document更改为var行,它们只显示消息,而不显示请求的页面我更改了上面的答案,请检查。问题是我只有一个js文件,可以在js文件中工作。我没有可以粘贴div标记的html文件。@M.Max:你不是从html中获取节点吗?:var node=document.getElementByIdmain-panel;您确实有一个具有主面板id的元素。