jQuery - 如何在跨度中包装字符串中的每个字符

时间:2022-11-29 10:46:24

How can I convert the characters of a div into spans?

如何将div的字符转换为span?

For example, I'd like to convert this:

例如,我想转换这个:

<div>
    Hello World
</div>

Into this:

进入这个:

<div>
    <span>H</span>
    <span>e</span>
    <span>l</span>
    <span>l</span>
    <span>o</span>
    <span>W</span>
    <span>o</span>
    <span>r</span>
    <span>l</span>
    <span>d</span>
</div>

I've tried this * suggestion, but that converts spaces into spans. What I need is to convert only characters to spans:

我尝试过这个*建议,但是将空格转换为跨度。我需要的是只将字符转换为跨度:

$("div").each(function (index) {
    var characters = $(this).text().split("");

    $this = $(this);
    $this.empty();
    $.each(characters, function (i, el) {
        $this.append("<span>" + el + "</span");
    });
 });

6 个解决方案

#1


2  

You can use String#replace method and html() method with a callback to reduce the code.

您可以使用String#replace方法和html()方法与回调来减少代码。

$("div").html(function(index, html) {
  return html.replace(/\S/g, '<span>$&</span>');
});

$("div").html(function(index, html) {
  return html.replace(/\S/g, '<span>$&</span>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Hello World
</div>

#2


1  

I'd prefer to use regular expression:

我更喜欢使用正则表达式:

var txt = $('#container').text();
var newTxt = txt.replace(/\w/g,function(c){
  return '<span>'+c+'</span>';
})
$('#container').html(newTxt);
span {
  display:inline-block;
  background-color:#dfdfdf;
  color:#aaa;
  padding:3px;
  margin:3px;
  border-radius:3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
    Hello World
</div>

#3


1  

You can try with this simple JavaScript.

您可以尝试使用这个简单的JavaScript。

(function() {
  var div, i, span = "";
  div = document.querySelectorAll("div")[0];
  for (i = 0; i < div.innerText.length; i++) {
    if (div.innerText[i] !== " ") {
      span += "<span>";
      span += div.innerText[i];
      span += "</span>";
    }
  }
  div.innerHTML = span;
})();
<div>
  Hello World
</div>

#4


0  

$("div").each(function (index) {
var characters = $(this).text().split("");

$this = $(this);
$this.empty();
$.each(characters, function (i, el) {
if(el != ' ')
$this.append("<span>" + el + "</span");
});

put a condition for space

为空间设置条件

#5


0  

try:

尝试:

$("div").each(function (index) {
  var characters = $(this).text().split("");
  characters = characters.filter(v => v != '');
  $(this).empty();
  for(var i =0; i < characters.length; i++) {
      $(this).append("<span>" + characters[i] + "</span");
  }    
});

#6


0  

tried to write as little as I can

尽量少写

html

HTML

<div>
    HelloWorld
</div>

js

JS

var d=$("div");
var text=d.text();
text=$.trim(text);
d.empty();

for(i=0;i<text.length;i++){
    var span=$("<span></span>");
    span.text(text[i]);
    d.append(span)
}

#1


2  

You can use String#replace method and html() method with a callback to reduce the code.

您可以使用String#replace方法和html()方法与回调来减少代码。

$("div").html(function(index, html) {
  return html.replace(/\S/g, '<span>$&</span>');
});

$("div").html(function(index, html) {
  return html.replace(/\S/g, '<span>$&</span>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Hello World
</div>

#2


1  

I'd prefer to use regular expression:

我更喜欢使用正则表达式:

var txt = $('#container').text();
var newTxt = txt.replace(/\w/g,function(c){
  return '<span>'+c+'</span>';
})
$('#container').html(newTxt);
span {
  display:inline-block;
  background-color:#dfdfdf;
  color:#aaa;
  padding:3px;
  margin:3px;
  border-radius:3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
    Hello World
</div>

#3


1  

You can try with this simple JavaScript.

您可以尝试使用这个简单的JavaScript。

(function() {
  var div, i, span = "";
  div = document.querySelectorAll("div")[0];
  for (i = 0; i < div.innerText.length; i++) {
    if (div.innerText[i] !== " ") {
      span += "<span>";
      span += div.innerText[i];
      span += "</span>";
    }
  }
  div.innerHTML = span;
})();
<div>
  Hello World
</div>

#4


0  

$("div").each(function (index) {
var characters = $(this).text().split("");

$this = $(this);
$this.empty();
$.each(characters, function (i, el) {
if(el != ' ')
$this.append("<span>" + el + "</span");
});

put a condition for space

为空间设置条件

#5


0  

try:

尝试:

$("div").each(function (index) {
  var characters = $(this).text().split("");
  characters = characters.filter(v => v != '');
  $(this).empty();
  for(var i =0; i < characters.length; i++) {
      $(this).append("<span>" + characters[i] + "</span");
  }    
});

#6


0  

tried to write as little as I can

尽量少写

html

HTML

<div>
    HelloWorld
</div>

js

JS

var d=$("div");
var text=d.text();
text=$.trim(text);
d.empty();

for(i=0;i<text.length;i++){
    var span=$("<span></span>");
    span.text(text[i]);
    d.append(span)
}