简体   繁体   English

如何使用jQuery或javascript分割多个字符串?

[英]How to split multiple string using jQuery or javascript?

I know this been posted here: how to split the string using jquery or javascript 我知道这已发布在这里: 如何使用jquery或javascript拆分字符串

but in my case have multiple strings. 但就我而言,有多个字符串。 It's working in a single line of string but if it's in a multiple lines it repeats the day after year. 它以单行字符串工作,但如果是多行,则日复一日地重复。 and for some reason it display's only the first 'li' value. 由于某种原因,它仅显示第一个“ li”值。 Is it possible to display it this way: 是否可以这样显示:

<ul>
<li>
    <div class="date">
        <p class='day'>23</p>
        <p class='month'>05</p>
        <p class='year'>2013</p>
    </div>
</li>
<li>
    <div class="date">
        <p class='day'>25</p>
        <p class='month'>07</p>
        <p class='year'>2014</p>
    </div>
</li>
<li>
    <div class="date">
        <p class='day'>01</p>
        <p class='month'>05</p>
        <p class='year'>2014</p>
    </div>
</li>
</ul>

here is my code: 这是我的代码:

html html

<ul> <li><div class="date">23-05-2013</div></li> <li><div class="date">25-07-2014</div></li> <li><div class="date">01-05-2014</div></li> </ul>

css: CSS:

.day{color:#ccc;}
.month{color:#ff0000;}
.year{color:green;}

script: 脚本:

var data =$('.date').text();
var arr = data.split('-');
$(".date").html("<p class='day'>"+arr[0]+"</p>"+"<p class='month'>"+arr[1]+"</p>"+"<p cass='year'>"+arr[2]+"</p>");

jsfiddle: jsfiddle:

demo 演示

thanks Bon 谢谢邦

You are getting the text from the first element, and changes all elements to contain the code for that. 您将从第一个元素获取文本,并更改​​所有元素以包含该代码。 You need to loop through the elements and convert the content in each one. 您需要遍历元素并转换每个元素中的内容。

You can use a callback function in the html method to do that, it will get the original HTML code from each element, and you return the new HTML code for that element: 您可以在html方法中使用回调函数来执行此操作,它将从每个元素获取原始HTML代码,然后返回该元素的新HTML代码:

$(".date").html(function(i, h) {
    var arr = h.split('-');
    return "<p class='day'>"+arr[0]+"</p><p class='month'>"+arr[1]+"</p><p class='year'>"+arr[2]+"</p>";
});

Demo: http://jsfiddle.net/Guffa/815c95jn/1/ 演示: http : //jsfiddle.net/Guffa/815c95jn/1/

(Note the difference in function, as this will get the HTML code in each element instead of the text. As long as there is no actual HTML markup in the elements, like in your example, there is no difference in the result.) (请注意功能上的差异,因为这将在每个元素而不是文本中获得HTML代码。只要元素中没有实际的HTML标记(如您的示例中那样),结果就不会有所不同。)


An alternative to splitting the text is to use replace : 拆分文本的另一种方法是使用replace

$(".date").html(function(i, h) {
    return "<p class='day'>" + h.replace("-", "</p><p class='month'>").replace("-", "</p><p class='year'>") + "</p>";
});

You only selected one .date , but you have to iterate over all of them, eg using $.each() : 您只选择了一个.date ,但是必须遍历所有$.each() ,例如使用$.each()

$(".date").each(function () {
  var data = $(this).text();
  var arr = data.split('-');
  $(this).html("<p class='day'>" + arr[0] + "</p>" + 
    "<p class='month'>" + arr[1] + "</p>" + "<p class='year'>" + arr[2] + "</p>");
});

Adjusted Fiddle , and for reference: http://api.jquery.com/each/ 调整了Fiddle ,供参考: http : //api.jquery.com/each/

Since the title asks about how to completed this with jQuery or Javascript (assuming vanilla JS) let me give a quick example of how this might be done without the need for jQuery: 由于标题询问了如何使用jQuery或Javascript(假设使用香草JS)完成此操作,因此让我举一个简单的示例,说明如何无需jQuery来完成此操作:

 var dates = document.querySelectorAll('.date'); var dateClasses = ['day', 'month', 'year']; Array.prototype.forEach.call(dates, function(date){ var dateString = date.innerHTML; var dateStringArray = dateString.split('-'); var content = ""; for(var i = 0; i < dateStringArray.length; i++){ var newDate = document.createElement('p'); newDate.classList.add(dateClasses[i]); newDate.innerHTML = dateStringArray[i]; content += newDate.outerHTML; } date.innerHTML = content; }); 
 .day{color:#ccc;} .month{color:#ff0000;} .year{color:green;} 
 <ul> <li><div class="date">23-05-2013</div></li> <li><div class="date">25-07-2014</div></li> <li><div class="date">01-05-2014</div></li> </ul> 

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

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