代码之家  ›  专栏  ›  技术社区  ›  SteinTech

尝试在body元素的顶部创建div元素

  •  0
  • SteinTech  · 技术社区  · 7 年前

    我用prepend错了吗?还是一定要用文档.createElement?

    function InitModal() {
        $("body").prepend("<div id=\"blackout\"></div>");
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Itay Gal    7 年前

    您需要调用函数,否则它将无法运行。我在页面加载上添加了一个调用。还有,你的报价有误。如果你用 ' 使用 "

    function InitModal() {
        $('body').prepend('<div id="blackout">New div</div>');
    }
    
    $(document).ready(function() {
        InitModal();
    });
    #original{
      background-color: #e5e5e5;
      border-radius: 4px;
      padding: 10px;
      text-align: center;
      margin-top: 10px;
    }
    #blackout{
      background-color: #A6F1CD;
      border-radius: 4px;
      padding: 10px;
      text-align: center;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="original">Original div</div>
        2
  •  0
  •   edi gerrit    7 年前

    InitModal() . 然后您将在dev工具中看到div已经创建。

    只要把 alert('called'); 你就会明白。