javascript - 替换js中的元素时HTMLCollection显示?
问题描述
我正在尝试将 Html(Div) 替换为文本元素首先我创建了您可以在此处看到的条件
html
<!-- Replace Text Here-->
<h6 class="idk1"> Replace This </h6>
<!-- Main Column -->
<div class="js-cp-main-wrp">
<div class="support-2">
<html>stuff here</html>
</div>
<div class="end-cp-wrp">
<html>stuff here</html>
<html>stuff here</html>
<html>stuff here</html>
</div>
</div>
脚本
var woahtikcets = document.getElementsByClassName('js-cp-main-wrp');
const loadthetickets = document.getElementsByClassName('idk1');
运行这个
loadthetickets.replaceWith(woahtikcets)
它告诉我 [object HTMLCollection] ??
这种方法不起作用它叹为未定义?请帮助<3
const loadthetickets = document.getElementsByClassName('idk1')[0];
解决方案
我完全不确定您的意图是什么,但<html>
不仅仅是HTML 中的一些标签 。它必须是外部标签(文档的根),因此只能在文档中出现一次。因此,如果您将<html>
元素替换为其他(合法)实体,<span>
您很可能会得到一些结果,如下所示:
const woahtikcets = document.querySelectorAll('.js-cp-main-wrp span'),
newtext = document.getElementsByClassName('idk1')[0].textContent;
for (var el of woahtikcets){el.textContent=newtext}
<!-- Replace Text Here-->
<h6 class="idk1"> Replace This </h6>
<!-- Main Column -->
<div class="js-cp-main-wrp">
<div class="support-2">
<span>stuff here</span>
</div>
<div class="end-cp-wrp">
<span>stuff here</span>
<span>stuff here</span>
<span>stuff here</span>
</div>
</div>
如果您希望替换操作以相反的方向工作,您可以执行以下操作:
const newtext = document.querySelectorAll('.js-cp-main-wrp')[0].textContent,
heading = document.getElementsByClassName('idk1');
for (var el of heading){el.textContent=newtext}
<!-- Replace Text Here-->
<h6 class="idk1"> Replace This </h6>
<!-- Main Column -->
<div class="js-cp-main-wrp">
<div class="support-2">
<span>stuff here</span>
</div>
<div class="end-cp-wrp">
<span>stuff here</span>
<span>stuff here</span>
<span>stuff here</span>
</div>
</div>
推荐阅读
- python - 此代码应该为每个场景生成随机的不同时间。但是,我得到了同样的时间。有没有更好的方法来做到这一点?
- bash - 是卷曲吗?它是一个帖子吗?从 bash 脚本“粘贴”到网站
- python - 无法开始使用 VSCode 和 Python 3.9
- javascript - 在 vue.js 中设置过滤器
- python - Turtle 不会在 PyCharm 中绘图
- mysql - 无法从其他容器连接到 MySQL 容器
- python - 如何让 Pylance 忽略 None 的可能性?
- java - Java Firebase 获取 RealtimeDatabase 值
- apache-spark - --master local[K] 和 --conf spark.executor.cores 如何确定线程数?
- python - 使用 Rust 的 `run_cmd` 在 `glob` 返回的迭代器上运行脚本