【问题标题】:How to add HTML append javascript... "quotation marks" problems如何添加 HTML 附加 javascript...“引号”问题
【发布时间】:2021-07-09 00:48:36
【问题描述】:

我正在尝试将 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>
  • 我想删除每个 div strong 上的“引号”,这是一个字符串。
  • 解决“引号”后,我必须删除完整的 img 标签 问题

【问题讨论】:

    标签: javascript html jquery append


    【解决方案1】:

    使用createElement创建dom元素到appendChildremoveChild删除元素。

    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>

    【讨论】:

      【解决方案2】:

      只需使用replaceChild() 方法

      img.src 有一个陷阱,因为你得到了一个 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>

      【讨论】:

        【解决方案3】:
        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);
        }
        

        【讨论】:

          【解决方案4】:

          在原始 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)
          })
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-11-24
              • 1970-01-01
              • 2012-09-18
              • 1970-01-01
              • 1970-01-01
              • 2023-01-13
              相关资源
              最近更新 更多