简体   繁体   English

JavaScript 中的 Append 元素

[英]Append element in JavaScript

I have the below HTML structure.我有下面的 HTML 结构。

<div class="SomeClassName">
   <a href="https://abc.xyz">
      <span>
         <img id="SomeImgID1" />
      </span>
   </a>
   <a href="https://xyz.abc">
      <span>
         <img id="SomeImgID2" />
      </span>
   </a>
</div>

I want to append <div> tag before each <img> tag, how do I do it with pure javascript?我想在每个<img>标签之前加上 append <div>标签,我该如何用纯 javascript 做呢?

Something like就像是

let divElement=document.querySelection('div.someClass');
Array.from(divElement.querySelectionAll('img'))
.forEach(el=>divElement.insertBefore(document.createElement('div'),el));

Again, if you want to wrap img then you can use next snippet:同样,如果你想包装img那么你可以使用下一个片段:

let divElement=document.querySelection('div.someClass');
Array.from(divElement.querySelectionAll('img'))
.forEach(el=>{
    let div=document.createElement('div');
    divElement.insertBefore(div,el));
    div.appendChild(el);
});

For more information seeElement.insertBefore on MDN有关更多信息,请参阅MDN 上的 Element.insertBefore

iaMsUPerNOva finally i got you and i am here with solution. iaMsUPerNova 终于找到了你,我在这里找到了解决方案。

<a class="image" href="https://abc.xyz"> <!--note this-->

$(function() {
        var node = $("a.image"); // This will copy your a tag so that img tag will also get copied.
    $(".SomeClassName").append('<div>hello</div>');//This will create a new div containing  hello text 
        $("a.image").remove(); // This will remove a tag which is already copied to node.
        $("SomeClassName").append(node); //Finally we are going to add newly created div to SomeClassName.  
    });

Okay, so what we want to do here involves two steps:好的,所以我们在这里要做的涉及两个步骤:

  1. Find each IMG element inside your DIV with class SomeClassName .使用 class SomeClassName在您的DIV中查找每个IMG元素。
  2. For each of these IMG elements, create a new DIV and insert it before the element.对于这些IMG元素中的每一个,创建一个新的DIV并将其插入到元素之前。

Here is how you do that.这是你如何做到的。

/* document.querySelectorAll returns a NodeList.
   The spread operator "..." converts it into an array,
   so that we can call the forEach method on it. */
[...document.querySelectorAll("div.SomeClassName img")].forEach(x => {
    // Here we create the new DIV
    let div = document.createElement("div");
    // Just some sample text to show
    div.textContent = "Hello!";
    // x.parentNode finds the parent of the IMG element.
    // The frst parameter of node.InsertBefore is the
    // element we want to insert as its child, and the
    // the second parameter is the reference element
    // before which this new element will be inserted
    x.parentNode.insertBefore(div, x);
});

Thank you, everyone, for your responses.谢谢大家的回复。 I forgot to mention before that I had another parent <div> and the final structure was like this.我之前忘了提到我还有另一个父<div>并且最终的结构是这样的。

<div class="ParentClassName">
   <div class="ChildClassName">Some Content</div>
   <div class="ChildClassName">Some Content2</div>
   <div class="ChildClassName">
      <a href="https://abc.xyz">
      <span>
      <img id="SomeImgID1" />
      </span>
      </a>
      <a href="https://xyz.abc">
      <span>
      <img id="SomeImgID2" />
      </span>
      </a>
   </div>
</div>

I could able to wrap all <img> tags inside <div> by doing the below step.通过执行以下步骤,我可以将所有<img>标签包装在<div>中。 Note: If your classname is dynamic you can remove it from the query selectors and you can put only the selector element like document.querySelector("div");注意:如果您的类名是动态的,您可以将其从查询选择器中删除,并且您只能放置选择器元素,例如document.querySelector("div");

 let divElement = document.querySelector("div.ParentClassName");
      Array.from(divElement.querySelectorAll("div.ChildClassName img")).forEach(
        (el) => {
          let div = document.createElement("div");
          el.parentElement.insertBefore(div, el);
          div.appendChild(el);
        }
      );

and my final output was我最后的 output 是

<div class="ParentClassName">
   <div class="ChildClassName">Some Content</div>
   <div class="ChildClassName">Some Content2</div>
   <div class="ChildClassName">
      <a href="https://abc.xyz">
         <span>
            <div>
               <img id="SomeImgID1" />
            </div>
         </span>
      </a>
      <a href="https://xyz.abc">
         <span>
            <div>
               <img id="SomeImgID2" />
            </div>
         </span>
      </a>
   </div>
</div>

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

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