首页 > 解决方案 > 带有 DOM 的 Jquery $.each 循环,不能适用于所有元素

问题描述

我正在尝试制作一个具有额外标记功能的 HTML 编辑器

在这个例子中,我使用 jQuery $.each 循环

但是这个循环只适用于最后一个元素

有我的代码

// Svg Icon

const LinkIcon = $('<svg fill="red" xmlns="http://www.w3.org/2000/svg" width="25px" viewBox="0 0 512 512"><path d="M326.612 185.391c59.747 59.809 58.927 155.698.36 214.59-.11.12-.24.25-.36.37l-67.2 67.2c-59.27 59.27-155.699 59.262-214.96 0-59.27-59.26-59.27-155.7 0-214.96l37.106-37.106c9.84-9.84 26.786-3.3 27.294 10.606.648 17.722 3.826 35.527 9.69 52.721 1.986 5.822.567 12.262-3.783 16.612l-13.087 13.087c-28.026 28.026-28.905 73.66-1.155 101.96 28.024 28.579 74.086 28.749 102.325.51l67.2-67.19c28.191-28.191 28.073-73.757 0-101.83-3.701-3.694-7.429-6.564-10.341-8.569a16.037 16.037 0 0 1-6.947-12.606c-.396-10.567 3.348-21.456 11.698-29.806l21.054-21.055c5.521-5.521 14.182-6.199 20.584-1.731a152.482 152.482 0 0 1 20.522 17.197zM467.547 44.449c-59.261-59.262-155.69-59.27-214.96 0l-67.2 67.2c-.12.12-.25.25-.36.37-58.566 58.892-59.387 154.781.36 214.59a152.454 152.454 0 0 0 20.521 17.196c6.402 4.468 15.064 3.789 20.584-1.731l21.054-21.055c8.35-8.35 12.094-19.239 11.698-29.806a16.037 16.037 0 0 0-6.947-12.606c-2.912-2.005-6.64-4.875-10.341-8.569-28.073-28.073-28.191-73.639 0-101.83l67.2-67.19c28.239-28.239 74.3-28.069 102.325.51 27.75 28.3 26.872 73.934-1.155 101.96l-13.087 13.087c-4.35 4.35-5.769 10.79-3.783 16.612 5.864 17.194 9.042 34.999 9.69 52.721.509 13.906 17.454 20.446 27.294 10.606l37.106-37.106c59.271-59.259 59.271-155.699.001-214.959z"/></svg>')[0];


      $("textarea").on("keyup", () => {
        MarkDown();
        myFunc();
      });
      function myFunc() {
        $.each($(".markdownOutput h1"), function (event) {
          this.prepend(LinkIcon);
        });
      }
      function MarkDown() {
        $(".markdownOutput").html($("textarea").val());
      }
      MarkDown();
      myFunc();
textarea{
  width: 100%;
  height: 100px;
  overflow: auto;
  border: none;
  border-left: 5px solid green;
  background: #eee;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <title>Document</title>
  </head>
  <body>
    <textarea><h1>hello World</h1>
<h1>hello World</h1>
<h1>hello World</h1>
</textarea>
    <div class="markdownOutput"></div>
  </body>
</html>

对问题的任何建议

标签: javascripthtmljqueryloopsforeach

解决方案


您不需要遍历每个元素。你可以这样做:

$(".markdownOutput>h1").prepend(LinkIcon);

// Svg Icon

const LinkIcon = $('<svg fill="red" xmlns="http://www.w3.org/2000/svg" width="25px" viewBox="0 0 512 512"><path d="M326.612 185.391c59.747 59.809 58.927 155.698.36 214.59-.11.12-.24.25-.36.37l-67.2 67.2c-59.27 59.27-155.699 59.262-214.96 0-59.27-59.26-59.27-155.7 0-214.96l37.106-37.106c9.84-9.84 26.786-3.3 27.294 10.606.648 17.722 3.826 35.527 9.69 52.721 1.986 5.822.567 12.262-3.783 16.612l-13.087 13.087c-28.026 28.026-28.905 73.66-1.155 101.96 28.024 28.579 74.086 28.749 102.325.51l67.2-67.19c28.191-28.191 28.073-73.757 0-101.83-3.701-3.694-7.429-6.564-10.341-8.569a16.037 16.037 0 0 1-6.947-12.606c-.396-10.567 3.348-21.456 11.698-29.806l21.054-21.055c5.521-5.521 14.182-6.199 20.584-1.731a152.482 152.482 0 0 1 20.522 17.197zM467.547 44.449c-59.261-59.262-155.69-59.27-214.96 0l-67.2 67.2c-.12.12-.25.25-.36.37-58.566 58.892-59.387 154.781.36 214.59a152.454 152.454 0 0 0 20.521 17.196c6.402 4.468 15.064 3.789 20.584-1.731l21.054-21.055c8.35-8.35 12.094-19.239 11.698-29.806a16.037 16.037 0 0 0-6.947-12.606c-2.912-2.005-6.64-4.875-10.341-8.569-28.073-28.073-28.191-73.639 0-101.83l67.2-67.19c28.239-28.239 74.3-28.069 102.325.51 27.75 28.3 26.872 73.934-1.155 101.96l-13.087 13.087c-4.35 4.35-5.769 10.79-3.783 16.612 5.864 17.194 9.042 34.999 9.69 52.721.509 13.906 17.454 20.446 27.294 10.606l37.106-37.106c59.271-59.259 59.271-155.699.001-214.959z"/></svg>')[0];


$("textarea").on("keyup", () => {
  MarkDown();
  myFunc();
});

function myFunc() {
  $(".markdownOutput>h1").prepend(LinkIcon);
}

function MarkDown() {
  $(".markdownOutput").html($("textarea").val());
}
MarkDown();
myFunc();
textarea {
  width: 100%;
  height: 100px;
  overflow: auto;
  border: none;
  border-left: 5px solid green;
  background: #eee;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
  <title>Document</title>
</head>

<body>
  <textarea><h1>hello World</h1>
<h1>hello World</h1>
<h1>hello World</h1>
</textarea>
  <div class="markdownOutput"></div>
</body>

</html>


推荐阅读