简体   繁体   English

如何使用javascript指定函数在文档中的位置?

[英]How to specify the location of a function inside a document with javascript?

I have a function to write an element inside an html document as follows: 我有一个函数可以在html文档中编写一个元素,如下所示:

function writeFooter(){
return "<div id='div3'><div class='down'>This is my footer</div></div>";}
self.document.writeln (writeFooter ());

The script is written in a document called footer.js and added at the head of the document as follows: 该脚本写在名为footer.js的文档中,并按如下所示添加在文档的开头:

<script src="footer.js" type="text/javascript"></script>

As the div id 3 is a footer it has the following style: 由于div id 3是页脚,因此具有以下样式:

#div3{position: relative; 
clear: both;}

The javascript function works properly but the element is written at the top of the document (right after the body) as below: javascript函数可以正常运行,但是元素写在文档顶部(紧随正文之后),如下所示:

<body>
<div id="div3"></div>
<div id="div1"></div>
<div id="div2"></div>
</body>

However, the div3 is meant to clear div1 and div2. 但是,div3用于清除div1和div2。 Is there a way I can specify where this function will be written inside the document so that I have the following result? 有没有一种方法可以指定此函数在文档中的写入位置,以便获得以下结果?

<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>

Thanks again! 再次感谢!

The script is written in a document called footer.js and added at the head of the document 该脚本写在名为footer.js的文档中,并添加到文档的开头。

That's the problem, because document.writeln outputs to the parsing stream at that point. 这就是问题所在,因为那时document.writeln输出到解析流。 Instead, include it at the location in the document where you want the footer to appear. 而是将其包括在文档中希望页脚出现的位置。 There are very, very few use cases for putting script elements in the head ; 有非常,非常少用例将script元素的head ; a footer script probably isn't among them. 页脚脚本可能不在其中。 :-) :-)

But if it absolutely has to be in the head , as you've tagged your question jQuery, you can use jQuery's ready callback to make your code wait until the document has been fully-parsed: 但是,如果它绝对必须head ,如标记了问题jQuery一样,则可以使用jQuery的ready回调使代码等待文档完全解析之后:

jQuery(function($) {
    $("<div id='div3'><div class='down'>This is my footer</div></div>").insertAfter("#div2");
});

document.writeln outputs into the document 'in place' when it is called. document.writeln在被调用时输出到“就地”文档中。 So if your script tags are in the head, or immediately after the body opening tag, that's where the text will be inserted into the document. 因此,如果您的脚本标签在头部,或紧接在主体打开标签之后,则将文本插入到文档中。 Either move your script section, or use the DOM functions to insert the element exactly where you want it. 移动您的脚本部分,或使用DOM函数将元素精确插入所需的位置。

Try following things: 尝试以下操作:

1) Modify your footer.js as: 1)将footer.js修改为:

   function writeFooter()
   {
      return "<div id='3'><div class='down'>This is my footer</div></div>";
   }
   $(document).ready(function(){
       $("body").append(writeFooter());
   });

2) modify your HTML as : 2)将HTML修改为:

    <body>
      <div id="1">one</div>
      <div id="2">two</div>
   </body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript函数document.location工作不正常 - javascript function document.location working incorrectly 如何使用 JavaScript 在 Html 中指定下载位置 - How to specify download location in Html using JavaScript 如何使用Javascript指定XML文档的编码 - How to specify the encoding of an XML document using Javascript 如何在其中指定带有javascript函数的网址? - How to specify an url with a javascript function in it? 有没有一种方法可以在location.replace内添加document.url(用Java语言) - is there a way to add document.url inside location.replace (in Javascript) document.ready中的Javascript函数 - Javascript function inside document.ready (Vanilla Javascript)根据事件处理程序的位置将函数应用于文档区域 - (Vanilla Javascript) Applying a function to areas of the document depending on the location of the event handler javascript/jQuery 函数调用的行为很奇怪 (document.location.reload) - javascript/jQuery function call behaves weird (document.location.reload) 如何指定用于运行javascript&#39;document.ready&#39;的视图或控制器 - how to specify the view or controller for running javascript 'document.ready' 如何使用 JavaScript 在函数上指定源和目标? - How to specify a source and a target on the function using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM