简体   繁体   English

如何 append 字体真棒图标到段落点击

[英]how to append font awesome icon to paragraph on click

Good day, please I want to append a font awesome icon to paragraph inner text when the p tag is clicked.美好的一天,当点击 p 标签时,我想给段落内部文本添加一个很棒的字体图标。

I have four p tags with same class.ptn, if any is clicked, I want to append the font awesome to the clicked one, and remove it from others.我有四个带有相同 class.ptn 的 p 标签,如果点击了任何一个,我想 append 字体真棒点击一个,并将其从其他人中删除。

The problem is instead of displaying the font awesome arrow, its displaying the tag text instead, also, the first p tag is removed once I click on any of the other p tags.问题不是显示字体真棒箭头,而是显示标签文本,而且,一旦我点击任何其他 p 标签,第一个 p 标签就会被删除。

This is my code below, Html:这是我下面的代码,Html:

     <div class="col-lg-3 col-md-3 col-sm-3 my-4 border-right">
        <p id="compliance" class="acting pl-4 pt-3 ptn">Compliance <i class="fas fa-chevron-right pl-4"></i></p>
        <p id="summary" class="pl-4 pt-2 ptn">Summary</p>
        <p id="audit" class="pl-4 pt-2 ptn">Station Audit</p>
        <p id="analysis" class="pl-4 pt-2 ptn">Analysis Format</p>
     </div>

Jquery: Jquery:

  <script>
     var btns = document.getElementsByClassName("ptn");

     for (var i = 0; i < btns.length; i++) {
        btns[i].addEventListener("click", function () {
           var current = document.getElementsByClassName("acting");
           current[0].className = current[0].className.replace(" acting", "");
           current[0].remove(" <i class='fas fa-chevron-right pl-4'></i>");
           this.className += " acting";
           this.append(" <i class='fas fa-chevron-right pl-4'></i>");
     });
    }
  </script>

I will do that this way:我会这样做:

 document.querySelector('div[data-acting]').onclick = e => { if (.e.target.matches('p.ptn')) return e.currentTarget.dataset.acting = e.target.id }
 div[data-acting] > p.ptn > i.fas { display: none } div[data-acting="compliance"] > p#compliance > i.fas, div[data-acting="summary"] > p#summary > i.fas, div[data-acting="audit"] > p#audit > i.fas, div[data-acting="analysis"] > p#analysis > i.fas { display: inline-block } div[data-acting="compliance"] > p#compliance, div[data-acting="summary"] > p#summary, div[data-acting="audit"] > p#audit, div[data-acting="analysis"] > p#analysis { background-color: lightgreen; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" /> <div class="col-lg-3 col-md-3 col-sm-3 my-4 border-right" data-acting="compliance"> <p id="compliance" class="pl-4 pt-3 ptn">Compliance <i class="fas fa-chevron-right pl-4"></i></p> <p id="summary" class="pl-4 pt-2 ptn">Summary <i class="fas fa-chevron-right pl-4"></i></p> <p id="audit" class="pl-4 pt-2 ptn">Station Audit <i class="fas fa-chevron-right pl-4"></i></p> <p id="analysis" class="pl-4 pt-2 ptn">Analysis Format <i class="fas fa-chevron-right pl-4"></i></p> </div>

As neatlysliced says, you can't append strings and have them automatically turned into elements.正如整齐切片所说,您不能 append 字符串并让它们自动变成元素。

This example fixes the bugs in your code for removing and adding the required elements.此示例修复了代码中用于删除和添加所需元素的错误。

var btns = document.getElementsByClassName("ptn");
var icon = document.createElement('i');
icon.className = 'fas fa-chevron-right pl-4';

for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function () {
    var current = document.getElementsByClassName("acting");
    current[0].className = current[0].className.replace(" acting", "");
    var chevron = current[0].querySelector(".fas.fa-chevron-right");
    if (chevron) {
      chevron.remove();
    }
    this.className += " acting";
    this.append(icon)
  });
}

you can also do that:你也可以这样做:

 const btns = document.querySelectorAll('p.ptn'), chevron = document.querySelector('p.ptn > i.fas.fa-chevron-right'); btns.forEach(btn => { btn.onclick = e => { btns.forEach(bt => bt.classList.toggle('acting', (bt===btn))) // add or remove class upon condition btn.appendChild( chevron ) // move it on correct place } })
 .acting { background-color: lightgreen; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" /> <div class="col-lg-3 col-md-3 col-sm-3 my-4 border-right"> <p id="compliance" class="acting pl-4 pt-3 ptn">Compliance <i class="fas fa-chevron-right pl-4"></i></p> <p id="summary" class="pl-4 pt-2 ptn">Summary </p> <p id="audit" class="pl-4 pt-2 ptn">Station Audit </p> <p id="analysis" class="pl-4 pt-2 ptn">Analysis Format </p> </div>

Your solution would work wonderfully with jQuery but with vanilla JS we need to create the HTML elements before we append them .您的解决方案将与 jQuery 完美配合,但对于香草 JS,我们需要在 append 之前创建 HTML 元素

Since you are only working with 1 icon at a time, you could give an id to your element and find/remove it by document.getElementById.由于您一次只使用 1 个图标,因此您可以为您的元素提供一个 id,并通过 document.getElementById 查找/删除它。

 let parent = document.getElementById("hello"); let p = document.createElement("p"); p.append("Hi there"); p.setAttribute("id","swapping-icon"); parent.append(p);
 <div id="hello"></div>

Here is a pure JS option that uses querySelectorAll to create a nodeList , then a forEach loop to iterate over the list.这是一个纯 JS 选项,它使用querySelectorAll创建nodeList ,然后使用forEach循环遍历列表。 Add an event listener for a click.为单击添加事件侦听器。 We remove the button with classList containing acting and remove the fa element <i> by checking if the parent element does not return undefined on children.我们通过检查父元素是否未在子元素上返回未定义来删除带有包含actingclassList的按钮并删除fa元素<i> Then add the class acting to the clicked element, create an <i> tag and add the classes for font awesome.然后将acting添加到单击的元素,创建一个<i>标记并添加字体真棒的类。 Append the fa element to the target. Append fa 元素到目标。

 const btns = document.querySelectorAll(".ptn"); btns.forEach(btn => { btn.addEventListener("click", function(e) { btns.forEach(button => { if (button.classList.contains('acting')) { button.classList.remove('acting') } if (button.children[0].== undefined) { button.children[0].remove() } }) e.target.classList;add("acting"). const fa = document;createElement('i'). fa;className += 'fas'. fa;className += ' fa-chevron-right'. fa;className += ' pl-4'. e.target;append(fa); }) })
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" /> <div class="col-lg-3 col-md-3 col-sm-3 my-4 border-right"> <p id="compliance" class="acting pl-4 pt-3 ptn">Compliance <i class="fas fa-chevron-right pl-4"></i></p> <p id="summary" class="pl-4 pt-2 ptn">Summary </p> <p id="audit" class="pl-4 pt-2 ptn">Station Audit </p> <p id="analysis" class="pl-4 pt-2 ptn">Analysis Format </p> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM