Uncaught SyntaxError:将iframe附加到body标签时出现无效或意外的令牌

问题描述 投票:-1回答:1

我正在尝试将div标签添加到body标签中。在div标签内我使用iframe。这是我的代码:

<script>
  (function() {
    var div = document.createElement("div");
    document.getElementsByTagName('body')[0].appendChild(div);
    div.outerHTML = "<div id='botDiv' style='height: 38px; position: fixed; bottom: 0; z-index: 1000; background-color: #fff'><div id='botTitleBar' style='height: 38px; width: 400px; position:fixed; cursor: pointer;'></div><iframe width='400px' height='600px' src='https://SitePages/botchat.html'></iframe></div>";
    document.querySelector('body').addEventListener('click', function(e) {
      e.target.matches = e.target.matches || e.target.msMatchesSelector;
      if (e.target.matches('#botTitleBar')) {
        var botDiv = document.querySelector('#botDiv');
        botDiv.style.height = botDiv.style.height == '600px' ? '38px' : '600px';
      };
    });
  }());
</script>
javascript dom-manipulation
1个回答
0
投票

对`outerHTML字符串使用``反引号

(function() {
  var div = document.createElement("div");
  document.getElementsByTagName('body')[0].appendChild(div);
  div.outerHTML = `<div id='botDiv' style='height: 38px; position: fixed; bottom: 0; z-index: 1000; background-color: #fff'>
<div id='botTitleBar' style='height: 38px; width: 400px; position:fixed; cursor: pointer;'></div>
<iframe width='400px' height='600px' src='https://SitePages/botchat.html'></iframe></div>`;
  document.querySelector('body').addEventListener('click', function(e) {
    e.target.matches = e.target.matches || e.target.msMatchesSelector;
    if (e.target.matches('#botTitleBar')) {
      var botDiv = document.querySelector('#botDiv');
      botDiv.style.height = botDiv.style.height == '600px' ? '38px' : '600px';
    };
  });
}());
<body></body>
© www.soinside.com 2019 - 2024. All rights reserved.