javascript - 在顶部创建一个 div 将所有其他内容向下推

标签 javascript html

我正在编写一个 Firefox 插件,它在页面顶部显示一个 div,并将所有其他内容向下推。我尝试将其放在 head 标签之前,但似乎不起作用。

我的代码:

var allDivs = document.getElementsByTagName('div');
var headTag = document.getElementsByTagName('head')[0];

if (allDivs.mainplugindiv)
{
  alert("!!! Plugin LLIBrowser is running !!!");
}
else
{
  var div = document.createElement('div');
  div.id='mainplugindiv';
  div.style.position = 'absolute';
  div.style.zIndex = '100';
  div.style.width = '100%';
  div.style.height = '150px';
  div.style.background = '#313192';
  div.style.color = 'white';
  div.innerHTML="<script type=\"text/javascript\"   src="+self.options.angularLib+"></script>"+
"<script type=\"text/javascript\" src="+self.options.angularApp+"></script>"+
  "<form style=\"margin: 0 auto; left: 10px; top: 50px; width: 98%;align: middle; visibility: visible\">"+
    "<label for=\"video\" style=\"color: white\"> <input type=\"checkbox\" checked=\"checked\" name=\"video\" value=\"video\" id=\"video\" /> Video</label>"+
    "<label for=\"forum\" style=\"color: white\"> <input type=\"checkbox\" checked=\"checked\" name=\"forum\" value=\"forum\" id=\"forum\" /> Forum</label>"+
    "<img src="+self.options.closeImg+" alt=\"Help\" style=\"width:20px;height:20px;\" align=right onclick=\"var youSure=confirm('Are you sure you want to close the plugin?'); if (youSure){var element = document.getElementById('mainplugindiv');element.outerHTML = '';delete element;}\">"+
    "<img src="+self.options.helpImg+" alt=\"Close\" style=\"width:20px;height:20px;\" align=right onclick=\"alert('LLIBrowser')\">"+
  "</form>"+
  "<div style=\"margin: 0 auto; left: 10px; top: 50px; width: 98%; height: 50%; align: middle; background-color:#75bbf7; visibility: visible\">"+
    "<div ng-show=\"tab == 1\">"+
      "<form>"+
        "<h3 style=\"color: white; align: center\">Video1 Video2 Video3 Video4</h3>"+
      "</form>"+
    "</div>"+
    "<div ng-show=\"tab == 2\">"+
      "<form>"+
        "<h3 style=\"color: white; align: center\">Forum1 Forum2 Forum3 Forum4</h3>"+
      "</form>"+
    "</div>"+
  "</div>"+
  "<form style=\"margin: 0 auto; left: 10px; top: 50px; width: 98%;align: middle; background-color:#313192; visibility: visible\">"+
  "<a href=\"#video\" style=\"color: white; background-color: #75bbf7; height: 15px; padding: 5 auto\" ng-click=\"tab = 1\">Video</a>"+
  "<a href=\"#forum\" style=\"color: white; background-color: #313192; height: 15px; padding: 5 auto\" ng-click=\"tab = 2\">Forum</a>"+
  "</form>";
  headTag.parentNode.insertBefore(div, headTag);

谢谢!

最佳答案

不要将位置设置为绝对,而是将其设置为相对并清除两侧。

div.style.position = 'relative';
div.style.clear = 'both';

这应该有效。

编辑:不要附加在 head 标签中,而是将其设为 yourbody 的第一个子标签

document.body.insertBefore(div,body.firstChild);

关于javascript - 在顶部创建一个 div 将所有其他内容向下推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111490/

相关文章:

javascript - 单击特定按钮时如何禁用正文滚动

javascript - 如何计算调整窗口大小的div背景图像大小

JQuery .load 弄乱了 HTML 文本样式

javascript - 如何获取 http.get 的值?

javascript - 在 Json 数组中添加新字段

javascript - Jquery addClass 导航不起作用

html - 避免 CSS 表达式

javascript - 渲染所有可能的元素或根据要求渲染

javascript - 创建新的 Mongoose 子文档时出现重复键错误

html - 将 2 张图像直接向右浮动,而不向下推文本