【问题标题】:Insert elements after each image with a specific class using plain javascript使用纯 javascript 在具有特定类的每个图像之后插入元素
【发布时间】:2018-02-04 12:25:56
【问题描述】:

我基本上是在尝试使用纯 JavaScript 在每个 img 元素下方添加一个标题,其类名为 img-caption。我找到了一些解决方案,但它们都使用 jQuery,我正在寻找一个普通的 javascript 解决方案。

我目前拥有的是这个不起作用的代码(从this question修改):

//insertAfter() needs to be updated
function insertAfter(referenceNode, newNode) {
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function inserCaption() {
  
  var imgs = document.getElementsByTagName("img");

  for (var i = 0; i < imgs.length; i++) {
    var NewSpan = document.createElement("span");
    var Caption = document.getElementsByTagName("img")[i].getAttribute("title");
    NewSpan.innerHTML = Caption;
    var ImgTag = document.getElementsByTagName("img");
    //I need a function inserAfter() to insert the titles after each image
    insertAfter(ImgTag, NewSpan);
  }
}
<img title="Hi" class="img-caption" src="http://i.imgur.com/KnX16nj.png"/>

<img title="Hello" class="img-caption" style="height:126px; width: auto;" src="http://i.imgur.com/naajTCH.png"/>

<button style="display:blocK" onclick="inserCaption()">Display Image Title</button>

到目前为止,我已经能够选择所有img 元素并获取它们的title 属性。我缺少的是选择具有特定类名img-captionimg更重要的是,如何实际插入相应的 title 每个属性之后 img 作为 span 元素。

由于我只修改了现有的作品,我真的不知道insertAfter() 是如何工作的。在 javascript 方面,我还不是初学者,只是为了澄清一下。

【问题讨论】:

  • 打开浏览器控制台并检查错误。如果您不了解javascript 但希望使用它,那么我建议您从您正在使用的 javascript 中学习一些基础知识或研究关键字,这可以帮助您理解这些函数中的操作。

标签: javascript html dom web-frontend


【解决方案1】:
function insertAfter(target, node) {
    let parent = target.parentNode;
    if(target.nextSibling !== null)
        parent.insertBefore(target.nextSibling, node);
    else
        parent.appendChild(node);
}

【讨论】:

  • 关于 CSS 解决方案。我认为伪元素不适用于 img,这就是我转向 javascript 的原因。我刚刚尝试过,它似乎没有任何效果,但如果不是这种情况,请告诉我。
  • @Dut 对不起,我的错,我什至没有测试 CSS 代码 ;_;
【解决方案2】:

function applyCaption(image, index) {
  const caption = document.createElement('span');
  
  caption.classList.add('caption');
  caption.textContent = image.title;

  
  return image
    .parentNode
    .insertBefore(caption, image.nextSibling)
  ;
}

function inserCaption() {
  
  return Array
    .prototype
    .forEach
    .call(document.querySelectorAll('img'), applyCaption)
  ;
}
<button style="display:blocK" onclick="inserCaption()">Display Image Title</button>
<hr />

<img title="Hi" class="img-caption" src="http://i.imgur.com/KnX16nj.png"/>
<img title="Hello" class="img-caption" style="height:126px; width: auto;" src="http://i.imgur.com/naajTCH.png"/>

【讨论】:

    【解决方案3】:

    您可以使用querySelectorAll 查找具有特定标签和类的元素,方法与jQuery相同:

    var imgs = document.querySelectorAll("img.img-caption");
    

    我也变了:

    insertAfter(imgs[i], NewSpan);
    

    //insertAfter() needs to be updated
    function insertAfter(referenceNode, newNode) {
      referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
    }
    
    function inserCaption() {
      var imgs = document.querySelectorAll("img.img-caption");
    
      for (var i = 0; i < imgs.length; i++) {
        var NewSpan = document.createElement("span");
        var Caption = imgs[i].getAttribute("title");
        NewSpan.innerHTML = Caption;
        
        insertAfter(imgs[i], NewSpan);
      }
    }
    <img title="Hi" class="img-caption" src="http://i.imgur.com/KnX16nj.png" />
    
    <img title="Hello" class="img-caption" style="height:126px; width: auto;" src="http://i.imgur.com/naajTCH.png" />
    
    <button style="display:blocK" onclick="inserCaption()">Display Image Title</button>

    【讨论】:

    • 这很好用,而且由于您没有对其进行大修,我理解所做的更改!我将如何向新创建的跨度添加一个类?现在我尝试添加NewSpan.className += " spanclass";,它确实有效,但看起来并不那么糟糕。
    • @Dut 您也可以使用NewSpan.classList.add("spanclass");,imo 与您的建议几乎相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2023-03-29
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    相关资源
    最近更新 更多