繁体   English   中英

如何在带有 Javascript 的 div 中包含标签 html

[英]How to include tags html inside div with Javascript

我有一个由几个标签组成的 html 源代码。 不幸的是,这些没有父容器,所以我想通过 Javascript 添加它,因为我不能直接对 html 结构进行操作。

现在,有了这个 function 我在其他元素之后添加标签 div (在我的例子中是输入)。

function insertAfter(referenceNode, newNode) {
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

var newDiv = document.createElement("div");
newDiv.setAttribute('class', 'example');
newDiv.setAttribute('id', 'new_div');

var input = document.getElementById("element_3");
insertAfter(input, newDiv);

我明白了,但这不是我想要做的。

<input type="checkbox" class="some_class" id="element_1">
<input type="checkbox" class="some_class" id="element_2">
<input type="checkbox" class="some_class" id="element_3">
<div class="exampmple" id="new_div"></div>

相反,我想要实现的是
现在我对 Javascript 比较陌生,并且希望作为粉丝学习新事物。 是否可以用 Js 或 jQuery 做我所描述的事情? 感谢您的帮助,感谢您的任何回复。

<div class="exampmple" id="new_div">
  <input type="checkbox" class="some_class" id="element_1">
  <input type="checkbox" class="some_class" id="element_2">
  <input type="checkbox" class="some_class" id="element_3">
</div>

更新:
这是我的实际 html 代码,我写的前一个是为了简化问题。

<label for="plugin_delete_me_shortcode_password">Password</label>
<input type="password" autocomplete="off" autofocus="autofocus" id="plugin_delete_me_shortcode_password" name="plugin_delete_me_shortcode_password">
<input type="checkbox" class="togglePw" id="pw_3" onclick="showPassword('plugin_delete_me_shortcode_password')">
<label for="pw_3" class="fa"></label>

这会更好

 function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } const newDiv = document.createElement("div"); newDiv.id = "new_div"; newDiv.classList.add("example"); const inputs = document.querySelectorAll(".some_class"); const lastInput = inputs[inputs.length - 1] insertAfter(lastInput, newDiv); inputs.forEach(inp => newDiv.append(inp))
 <input type="checkbox" class="some_class" id="element_1"> <input type="checkbox" class="some_class" id="element_2"> <input type="checkbox" class="some_class" id="element_3">

使用现有代码的示例 - 如果您可以更改代码,请执行此操作

 const showPassword = (id, show) => { document.getElementById(id).type = show? "text": "password" }; document.getElementById("pw_3").addEventListener("click", function() { showPassword('plugin_delete_me_shortcode_password', this.checked) }) function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } const newDiv = document.createElement("div"); newDiv.id = "new_div"; newDiv.classList.add("example"); const inputs = document.querySelectorAll("label"); const lastInput = inputs[inputs.length - 1] insertAfter(lastInput, newDiv); inputs.forEach(inp => newDiv.append(inp))
 .example { border: 1px solid black; width: 200px; padding: 10px; }
 <label>Password <input type="password" autocomplete="off" autofocus="autofocus" id="plugin_delete_me_shortcode_password" name="plugin_delete_me_shortcode_password"></label> <label class="fa"><input type="checkbox" class="togglePw" id="pw_3"> </label>

暂无
暂无

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

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