简体   繁体   English

本机Javascript不会在DOM上附加元素

[英]Native Javascript doesn't append element on the DOM

I have a website built in Expression Engine. 我有一个用Expression Engine构建的网站。 In the back-end there is a code snippet that takes care of a JavaScript request and build a page based on the request. 在后端,有一个代码段,用于处理JavaScript请求并根据该请求构建页面。

  1. I have a HTML Page without head tag. 我有一个没有head标签的HTML页面。
  2. This page is without styling 此页面没有样式

Sample: 样品:

<div class="top-arrow"><p><!--- Rest of code --></p>
</div>
<!-- Html page continues-->

I have added the following code in my attempt and it doesnt seem to work. 我尝试添加以下代码,但似乎无法正常工作。

var span = document.createElement("span"); //Test element
span.textContent = "A <span> element.";

var node = document.getElementsByClassName("top-arrow");
node.insertBefore(span);

Below is what I get: 以下是我得到的:

TypeError: node.insertBefore is not a function TypeError:node.insertBefore不是函数

node.insertBefore(span); node.insertBefore(span);

How best can I append text before the div with plain JavaScript. 如何最好地使用纯JavaScript在div之前附加文本。

getElementsByClassName will return array-like node-list which does not have method insertBefore getElementsByClassName将返回没有方法insertBefore array-like节点列表。

The Node.insertBefore(newNode, referenceNode) method inserts the specified node before the reference node as a child of the current node(If referenceNode is null, the newNode is inserted at the end of the list of child nodes) Node.insertBefore(newNode,referenceNode)方法将指定节点插入到引用节点之前,作为当前节点的子节点(如果referenceNode为null,则将newNode插入子节点列表的末尾)

Note: referenceNode is not an optional argument, if there is no ant ref node, pass null 注意: referenceNode不是可选参数,如果没有ant ref节点,则传递null

Try this: 尝试这个:

 var span = document.createElement("span"); span.textContent = "A <span> element."; var node = document.getElementsByClassName("top-arrow")[0]; //_____________________________________________________^^(Get the first element from collection) node.insertBefore(span, null); 
 <div class="top-arrow"> <p> </p> </div> 

document.getElementsByClassName("top-arrow") will return a live HTMLCollection . document.getElementsByClassName("top-arrow")将返回实时HTMLCollection You can use it like an array: 您可以像数组一样使用它:

node = document.getElementsByClassName("top-arrow")[0];

Also, if you want the new node to appear before top-arrow you need to do: 另外,如果您希望新节点出现 top-arrow 之前 ,则需要执行以下操作:

node.parentNode.insertBefore(span, node);

As it is node has no children, so there is no need to do insertBefore . 由于它是node没有子node ,因此无需执行insertBefore

Even though your HTML code has no body and head, the browser will 'fix' your HTML and add one. 即使您的HTML代码没有内容和头部,浏览器也会“修复”您的HTML并添加一个。

I would write your code like this: 我会这样写你的代码:

var span = document.createElement("span"); //Test element
span.appendChild(document.createTextNode("A <span> element."));

var node = document.getElementsByClassName("top-arrow")[0];
node.parentNode.insertBefore(span, node);

Function getElementsByClassName() returns an array containing nodes with class specified. 函数getElementsByClassName()返回一个包含指定类节点的数组。 If you want to insertBefore or append anything to it you need to specify index of an element in this array. 如果要insertBefore或附加任何内容,则需要指定此数组中元素的索引。 Also, insertBefore requires two arguments in function call (elementToInsert, elemenBeforeWhichYouWantToInsert). 另外,insertBefore在函数调用中需要两个参数(elementToInsert,elemenBeforeWhichYouWantToInsert)。 So, something like this should work: 因此,这样的事情应该起作用:

document.getElementsByClassName('top-arrow')[0].insertBefore(element, beforeWhatToInsert);

Thank you guys for all your input they are very informative. 谢谢大家的宝贵意见。 I have solved this without the need of manipulating my DOM element by simply copying the dynamic part of the page and actually creating a new template in the back-end of Expression Engine and my problem was solved. 通过简单地复制页面的动态部分并实际上在Expression Engine的后端中创建一个新模板,我无需操纵DOM元素即可解决此问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM