それはhtml要素を削除し、JavaScriptを使用してあなたの子供を維持することは可能ですか?

wDrik:

私は削除したいと思います..次のような構造を持っているdiv.sonが、維持div.grandson、その可能ですか?!またはあなたの変更<tag>も解決策になる。..例:から変更する<fieldset>には<div>、私はHTMLへのアクセスを持っていないことを思い出して、すべての変更は** ** JavaScriptを使用して行う必要があります!

<div class="father">
  <fieldset class="son">
    <div class="grandson">Content here</div>
    <div class="grandson">Content here</div>
    <div class="grandson">Content here</div>
    <div class="grandson">Content here</div>
  </fieldset>
</div>

私が使用してみましたremoveChild ()** **のjavascriptの機能を、それが全体の要素を削除します。

サムリ・Hakoniemi:

それは深いのノードクローニングすることによって、バニラJavaScriptで可能です孫を何かを削除する前に。その後、親に戻ってそれを付加します。あなたはどこか別の場所に置きたい場合はもちろん、あなたはDOMのトラバースの必要なロジックを追加する必要があります。(CSSセクションは、結果を視覚的に確認するためのものです)

const grandson = document.querySelector('.grandson');
const father = grandson.closest('.father');
const clonedGrandson = grandson.cloneNode(true);

father.querySelector('.son').remove();
father.appendChild(clonedGrandson);
.father {
  background-color: red;
  padding: 20px;
 }
 
 .son {
  background-color: blue;
  padding: 20px;
 }
 
 .grandson {
  background-color: green;
  padding: 20px;
 }
<div class="father">
  <fieldset class="son">
    <div class="grandson">
      <p>Save me</p>
    </div>
  </fieldset>
</div>

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=32383&siteId=1