如何删除在JavaScript中具有数据类型字符串的HTML元素?

发布于 2025-01-29 09:52:49 字数 1194 浏览 1 评论 0原文

我正在尝试重新设计测验模板,该模板只允许用户在禁用其他选项之前选择一个答案。用户应该能够在提交之前选择另一个答案(第一个删除)。

如果选择了一个新答案,我可以删除突出显示的颜色类:

answer.classList.remove("correct"); 

如何删除“ tick”图标的元素:

let tickIconTag = '<div class="icon tick"><i class="fas fa-check"></i></div>';

从此父元素中:

'<div class="option"><span>'+  multiple_choice_option[i] +'</span></div>'

当选项设置为这样:

  const option = option_list.querySelectorAll(".option");
  for(i=0; i < option.length; i++)
  {
    option[i].setAttribute("onclick", "optionSelected(this);");
  }//end for

添加tickicontag时:

answer.insertAdjacentHTML("beforeend",tickIconTag); 

我有尝试了多种方法,但主要问题是:

1。使用答案。Intertadjacenthtml(“ trefornend”,blankelement)向内移动tick tick未删除

2.使用tickicontag.remove()给出了错误“ tickicontag.remove.remove.remove不是函数”

3。使用答案。Removechild(tickicontag)给出错误“参数1不是类型节点”。

如何将同一元素确切设置为空白,而不将其移动或删除?

这是我尝试用空白替换元素时发生的情况。 绿色tick移动但未删除

I am trying to redesign a quiz template that only allows users to choose one answer before the other options are disabled. The user should be able to choose another answer (erasing the first) before submitting.

I can remove the highlighted color class if a new answer is chosen:

answer.classList.remove("correct"); 

How do I remove the element for the 'tick' icon:

let tickIconTag = '<div class="icon tick"><i class="fas fa-check"></i></div>';

from this parent element:

'<div class="option"><span>'+  multiple_choice_option[i] +'</span></div>'

when the option is set like this:

  const option = option_list.querySelectorAll(".option");
  for(i=0; i < option.length; i++)
  {
    option[i].setAttribute("onclick", "optionSelected(this);");
  }//end for

and the tickIconTag is added like this:

answer.insertAdjacentHTML("beforeend",tickIconTag); 

I have tried multiple ways but the main issue is that:

1.Using answer.insertAdjacentHTML("beforeend", blankelement) shifts tick inward not removed

2.Using tickIconTag.remove() gives the error "tickIconTag.remove is not a function"

3.Using answer.removeChild(tickIconTag) give the error "Parameter 1 is not of type node".

How can I set precisely the same element to blank and not shift it or remove it?

This is an image of what happens when I try to replace the element with a blank one.
Green tick shifted but not removed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2025-02-05 09:52:49

纯CSS解决方案

“ ...仅允许用户在禁用另一个选项之前选择一个答案。用户应该能够在提交之前选择另一个答案(第一个删除第一个)。”

这是< a href =“ https://developer.mozilla.org/en-us/docs/web/html/element/element/input/radio” rel =“ nofollow noreferrer”> radiogrouplist 。单击图I 中的每个圆圈。

图i

<input id='a1' name='A1' type='radio'><br>
<input id='b1' name='A1' type='radio'><br>
<input id='c1' name='A1' type='radio'><br>
<input id='d1' name='A1' type='radio'><br>

请注意,每个元素的名称是相同的a1。这允许一组广播按钮具有相互排他性

&lt; label&gt; a>使用每个广播按钮,以及一些CSS,我们可以在没有JavaScript的情况下重新创建OP中的内容。 &lt; label&gt; s具有独特的功能,其中它们可以与a表单控件通过为属性分配属性,并带有表单控件的#id的值。一旦关联了&lt; label&gt;是带有上述表单控制的Insanc同一页面。参见图II

图II

input {
  display: block;
  width: 2rem;
}

label {
  margin-top: 80px;
  display: block;
}

input:checked+label {
  color: gold;
  background: blue;
}
<input id='fc' type='checkbox'>
<label for='fc'>CLICK HERE</label>

在CSS中,以下规则集允许&lt; label&gt;每当检查复选框时更改:

图III III

input:checked+label {
  color: gold;
  background: blue;
}

in 图IV IV IV iv iv 每个> &lt; label&gt;与隐藏的广播按钮关联。用户只会看到&lt; label&gt;。同样,图IV 中的示例具有响应性。

图IV

html {
  font: 300 3vmax/1.2 'Segoe UI';
}

input {
  display: none
}

label {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 70vw;
  height: 2rem;
  margin: 0.5rem auto;
  padding: 0.5rem 1rem;
  border: 0.05rem solid cyan;
  border-radius: 4px;
  font: inherit;
  background: lightblue;
  cursor: pointer;
}

label b {
  position: absolute;
  display: inline-block;
  font-size: 1.2rem;
  font-weight: 300;
}

.far {
  position: absolute;
  left: 45%;
  top: 25%;
  display: inline-block;
  font-size: 1.75rem;
  color: lightblue;
}

input:checked+label {
  border-color: green;
  background: lightgreen;
}

input:checked+label i {
  color: green;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" rel="stylesheet">
<input id='a1' name='A1' type='radio' value='a' required><label for='a1'><b>Text for</b><i class="far fa-circle-check"></i></label>
<input id='b1' name='A1' type='radio' value='b'><label for='b1'><b>each answer</b><i class="far fa-circle-check"></i></label>
<input id='c1' name='A1' type='radio' value='c'><label for='c1'><b>goes into</b><i class="far fa-circle-check"></i></label>
<input id='d1' name='A1' type='radio' value='d'><label for='d1'><b>a label</b><i class="far fa-circle-check"></i></label>

Pure CSS Solution

"...only allows users to choose one answer before the other options are disabled. The user should be able to choose another answer (erasing the first) before submitting."

That's the exact behavior of a RadioGroupList. Click each circle in Figure I.

Figure I

<input id='a1' name='A1' type='radio'><br>
<input id='b1' name='A1' type='radio'><br>
<input id='c1' name='A1' type='radio'><br>
<input id='d1' name='A1' type='radio'><br>

Notice the name of each element is the same A1. This allows the group of radio buttons to have mutual exclusivity.

With a <label> associated with each radio button, and some CSS we can recreate what is in the OP without JavaScript. <label>s have a unique feature in which they can be associated with a form control by assigning the for attribute with the value of a form control's #id. Once associated a <label> is insync with said form control so that if the <label> was clicked, so to does the form control as long as both are on the same page. See Figure II.

Figure II

input {
  display: block;
  width: 2rem;
}

label {
  margin-top: 80px;
  display: block;
}

input:checked+label {
  color: gold;
  background: blue;
}
<input id='fc' type='checkbox'>
<label for='fc'>CLICK HERE</label>

In the CSS the following ruleset allowed the <label> to change whenever the checkbox was checked:

Figure III

input:checked+label {
  color: gold;
  background: blue;
}

In Figure IV each <label> is associated to a hidden radio button. The user will only see and interact with the <label>. Also, the example in Figure IV is responsive.

Figure IV

html {
  font: 300 3vmax/1.2 'Segoe UI';
}

input {
  display: none
}

label {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 70vw;
  height: 2rem;
  margin: 0.5rem auto;
  padding: 0.5rem 1rem;
  border: 0.05rem solid cyan;
  border-radius: 4px;
  font: inherit;
  background: lightblue;
  cursor: pointer;
}

label b {
  position: absolute;
  display: inline-block;
  font-size: 1.2rem;
  font-weight: 300;
}

.far {
  position: absolute;
  left: 45%;
  top: 25%;
  display: inline-block;
  font-size: 1.75rem;
  color: lightblue;
}

input:checked+label {
  border-color: green;
  background: lightgreen;
}

input:checked+label i {
  color: green;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" rel="stylesheet">
<input id='a1' name='A1' type='radio' value='a' required><label for='a1'><b>Text for</b><i class="far fa-circle-check"></i></label>
<input id='b1' name='A1' type='radio' value='b'><label for='b1'><b>each answer</b><i class="far fa-circle-check"></i></label>
<input id='c1' name='A1' type='radio' value='c'><label for='c1'><b>goes into</b><i class="far fa-circle-check"></i></label>
<input id='d1' name='A1' type='radio' value='d'><label for='d1'><b>a label</b><i class="far fa-circle-check"></i></label>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文