简体   繁体   English

JS中使用内部HTML切换段落

[英]Switch paragraphs using inner HTML in JS

I am trying to switch two paragraphs after clicking the button but I am stuck.单击按钮后,我试图切换两个段落,但我被卡住了。 Is there any way how to do this using inner HTML without using IF or boolean?有什么方法可以使用内部 HTML 而不使用 IF 或 boolean 来做到这一点? I tried this code but it doesn't work.我试过这段代码,但它不起作用。 Thanks谢谢

let elmsButton = document.querySelectorAll("button")[0];
let elmsParagraf1 = document.querySelectorAll(".prvni")[0];
let elmsParagraf2 = document.querySelectorAll(".druhy")[0];

elmsButton.addEventListener("click", () => {
    elmsParagraf1.innerHTML = "<div class='druhy'></div>"
    elmsParagraf2.innerHTML = "<div class='prvni'></div>"
});

Assign each DOM.innerHTML of paragraph to a variable then swap them like below example:将段落的每个DOM.innerHTML分配给一个variable ,然后像下面的例子一样交换它们:

 let elmsButton = document.querySelectorAll("button")[0]; let elmsParagraf1 = document.querySelectorAll(".prvni")[0]; let elmsParagraf2 = document.querySelectorAll(".druhy")[0]; elmsButton.addEventListener("click", () => { const p1 = elmsParagraf1.innerHTML; const p2 = elmsParagraf2.innerHTML elmsParagraf1.innerHTML = p2; elmsParagraf2.innerHTML = p1 });
 <button>Click</button> <div class='prvni'>Paragraph 1</div> <div class='druhy'>Paragraph 2</div>

You can switch those two divs by creating a temporary variable that holds Paragraf1 and then change Paragraf1 to Paragraf2 and vice versa, but with the variable.您可以通过创建一个保存 Paragraf1 的临时变量来切换这两个 div,然后将 Paragraf1 更改为 Paragraf2,反之亦然,但要使用该变量。

    let temp = elmsParagraf1.innerHTML;
    elmsParagraf1.innerHTML = elmsParagraf2.innerHTML
    elmsParagraf2.innerHTML = temp

Why don't you use querySelector in place of using querySelectorAll and choose the first element?为什么不使用querySelector而不是使用querySelectorAll并选择第一个元素?

By the way, I advise to delete and re-add the elements from the parent rather than using innerHTML .顺便说一句,我建议从父元素中删除并重新添加元素,而不是使用innerHTML The use of innerHTML would not preserve listeners and have worse performances:使用innerHTML不会保留听众并且性能更差:

let elmsButton = document.querySelector("button");
let elmsParagraf1 = document.querySelector(".prvni");
let elmsParagraf2 = document.querySelector(".druhy");

elmsButton.addEventListener("click", () => {
    swapElements(elmsParagraf1, elmsParagraf2);
});

function swapElements(elem1, elem2) {
  // Check if siblings
  if (elem1.parentElement !== elem2.parentElement) {
    console.error('No sibling elements!');
    return;
  }
  elem1.replaceWith(elem2.cloneNode(true));
  elem2.replaceWith(elem1.cloneNode(true));
}

Example:例子:

 let elmsButton = document.querySelector("button"); elmsButton.addEventListener("click", () => { let elmsParagraf1 = document.querySelector(".prvni"); let elmsParagraf2 = document.querySelector(".druhy"); swapElements(elmsParagraf1, elmsParagraf2); }); function swapElements(elem1, elem2) { // Check if siblings if (elem1.parentElement.== elem2.parentElement) { console;error('No sibling elements;'). return. } elem1;replaceWith(elem2.cloneNode(true)). elem2;replaceWith(elem1.cloneNode(true)); }
 <button>Click me</button> <div class="prvni">I am the first div</div> <div class="druhy">I am the second div</div>

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

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