javascript - JavaScript:如何在每个段落之后添加 iframe?
问题描述
我是 JavaScript 新手,期待专家的回答。
假设以下是我的内容-
<p> This is the first paragraph </p>
<p> This is the second paragraph </p>
而且,我想在每个段落之后添加一些代码块,并且代码如下 -
<p> This is the first paragraph </p>
<figure class="x">
<iframe width="300" height="250" style="border:0; margin:0;" src="#">
</iframe>
</figure>
<p> This is the second paragraph </p>
<figure class="x">
<iframe width="300" height="250" style="border:0; margin:0;" src="#">
</iframe>
</figure>
解决方案
你可以试试这个 javascript 来开始。
// get all "p" tags
var elements = document.getElementsByTagName('p');
for (var i = 0; i < elements.length; i++) {
// create figure
const figure = document.createElement("figure");
figure.className = "x";
// create iframe
const iframe = document.createElement("iframe");
iframe.setAttribute("width", "300");
iframe.setAttribute("height", "250");
iframe.setAttribute("href", "#");
iframe.style.border = 0;
iframe.style.margin = 0;
figure.appendChild(iframe);
// append to "p" tag
elements[i].parentNode.insertBefore(figure, elements[i].nextSibling);(figure);
}
推荐阅读
- angular - Angular Material点击事件不会触发
- android - 如何在 IO 线程中从房间数据库异步获取数据,并在主线程上使用 kotlin 中的 Flow 或 LiveData 更新 UI?
- mongodb - 带有 $sum 操作的 Mongodb 位置运算符“$”不起作用
- controller - C声音呼吸控制和声音字体
- ios - 如何在 SceneDelegate 中组合两个内容
- docker - 无法访问 Prometheus 仪表板/端口转发不起作用
- facebook - Facebook Business Manager - 您如何将关注者/喜欢从主要业务页面转移到商店页面?
- javascript - 前两个输入字段冻结的反应钩子表单,我无法单击前两个字段,在前两个输入字段之后它是可点击的
- python - 在 Python 中导出时如何避免空白行被 nan 替换
- sql - AWS Athena CTAS 查询失败,建议清空空桶