[英]How to change img alt src to div strong using javascript
I have a group of images inside a div, what I really want to do with javascript is change the src to <strong>
inside <div>
and delete the img alt="image"
我在一个 div 中有一组图像,我真正想要对 javascript 做的是将 src 更改为<div>
内的<strong>
> 并删除img alt="image"
example:例子:
I have this我有这个
<div class="multi-gallery-image show" id="service_preview">
<img alt="image" src="チャイルドカット¥3000">
<img alt="image" src="ジュニアカット">
<img alt="image" src="ハナコカット">
<img alt="image" src="Hair Styling">
<img alt="image" src="Manicures">
<img alt="image" src="Hair Coloring">
</div>
and I want to transform each img src to <div> <strong>
like this我想像这样将每个 img src 转换为<div> <strong>
<div class="multi-gallery-image show" id="service_preview">
<div><strong>チャイルドカット¥3000</strong></div>
<div><strong>ジュニアカット</strong></div>
<div><strong>トップスタイリストカット</strong></div>
<div><strong>Hair Styling</strong></div>
<div><strong>Manicures</strong></div>
<div><strong>Hair Coloring</strong></div>
</div>
at the moment I have this目前我有这个
let servicePreview = document.getElementById('service_preview');
console.log(servicePreview);
and the result is the same as the second code.结果与第二个代码相同。
How can I change img alt src
HTML tags to div strong
using javascript?如何使用 javascript 将img alt src
HTML 标签更改为div strong
? or jquery或 jquery
You need to get the <img>
elements from the DOM, then create a <div>
and <strong>
for each <img>
where the inner text of the <strong>
is the src
attribute of the <img>
.您需要从 DOM 中获取<img>
元素,然后为每个<img>
创建一个<div>
和<strong>
,其中<strong>
的内部文本是<img>
的src
属性。
No need for jQuery by the way, the JS DOM API can handle this just fine.顺便说一句,不需要 jQuery,JS DOM API 可以很好地处理这个问题。
Here's an example:这是一个例子:
const target = document.querySelector("#service_preview_divs"); const imgs = document.querySelectorAll("#service_preview img"); imgs.forEach(img => { const div = document.createElement("div"); const strong = document.createElement("strong"); strong.innerText = img.getAttribute("src"); div.appendChild(strong); target.appendChild(div); });
<div class="multi-gallery-image show" id="service_preview"> <img alt="image" src="チャイルドカット¥3000"> <img alt="image" src="ジュニアカット"> <img alt="image" src="ハナコカット"> <img alt="image" src="Hair Styling"> <img alt="image" src="Manicures"> <img alt="image" src="Hair Coloring"> </div> <div id="service_preview_divs"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.