如何添加HTML append javascript..."quotation marks"问题

How to add HTML append javascript... "quotation marks" problems

我正在尝试将 div 内容从 img 转换为 div strong

例如

<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 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>

我有这个但是结果和预期的不一样:

let servicePreview = document.getElementById('service_preview');     //parent where I am trying to introduce the src values
let myImg;
let mySrc;
let toPush;

if (servicePreview.getElementsByTagName('img').length > 0){
    let servicesNumber = servicePreview.getElementsByTagName('img').length;     //number of img tags inside the service_preview
    for (let i = 0; i < servicesNumber; i++){
        myImg = servicePreview.getElementsByTagName('img')[i];      //capturing the img tag
        mySrc = myImg.getAttribute('src');              //capturing the src value from img tag
        toPush = '<div><strong>' + mySrc + '</strong></div>';      //creating html tag for push to the parent service_preview
        servicePreview.append(toPush);                            //appending the html tag
    }
}

但这样做的结果是

<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="トップスタイリストカット">
    <img alt="image" src="Hair Styling">
    <img alt="image" src="Manicures">
    
    "<div><strong>チャイルドカット¥3000</strong></div>"
    "<div><strong>ジュニアカット</strong></div>"
    "<div><strong>ハナコカット</strong></div>"
    "<div><strong>トップスタイリストカット</strong></div>"
    "<div><strong>Hair Styling</strong></div>"
    "<div><strong>Manicures</strong></div>"
</div>

与 jQuery append() 不同,本机方法将 html 字符串视为文本

改用insertAdjacentHTML(position, html)

const str = '<div class="inserted">Test</div>'

document.getElementById('one').append(str);// shows as text
document.getElementById('two').insertAdjacentHTML('beforeend', str)
.inserted{color:red}
<div id="one"></div>
<div id="two"></div>

使用 createElement 创建 dom 元素到 appendChild 并使用 removeChild 删除元素。

let servicePreview = document.getElementById('service_preview');
var myImg;
var mySrc;
let toPush;

var elements = servicePreview.getElementsByTagName('img');
while (elements[0]) {
  newDiv = document.createElement('div');
  newStrong = document.createElement('strong');
  newStrong.innerHTML = elements[0].getAttribute('src');
  newDiv.appendChild(newStrong);
  servicePreview.appendChild(newDiv);
  elements[0].parentNode.removeChild(elements[0]);
}
<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>

原始 JavaScript 我相信它是这样的。

const services = ['チャイルドカット¥3000', 'ジュニアカット', 'ハナコカット',
  'Hair Styling', 'Manicures', 'Hair Coloring']

const preview = document.getElementById("service_preview")

services.forEach(service =>{
  const div = document.createElement("div")
  const strong = document.createElement("strong")
  strong.innerText = service
  div.appendChild(strong)
  preview.appendChild(div)
})

只需使用replaceChild()方法

there is a trap with img.src because you get an URI

const servicePreview = document.getElementById('service_preview')

servicePreview.querySelectorAll('img').forEach(imgElm=>
  {
  let newDiv  = document.createElement('div')
    , newStrg = document.createElement('strong')
    ;
  newDiv.appendChild( newStrg )
  newStrg.textContent = imgElm.getAttribute('src')  // or decodeURI( imgElm.src.split('/').pop() ) 

  servicePreview.replaceChild( newDiv, imgElm )
  })
<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>

const servicePreview = document.getElementById("service_preview");
const imageSources = [...servicePreview.children].map((img) => img.src);
console.log(imageSources);

// Remove all images
while (servicePreview.firstChild) {
  servicePreview.removeChild(servicePreview.firstChild);
}

// Add new div/strong tags
for (const imageSource of imageSources) {
  const div = document.createElement("div");
  const strong = document.createElement("strong");

  strong.textContent = imageSource;

  div.appendChild(strong);
  servicePreview.appendChild(div);
}