Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

javascript - 如何在第一个`/`(斜杠)中拆分一个字符串并在`<span>`中包围它的一部分?(How to split a string at the first `/` (slash) and surround part of it in a `<span>`?)

I want to format this date: <div id="date">23/05/2013</div> .(我想格式化这个日期: <div id="date">23/05/2013</div> 。)

First I want to split the string at the first / and have the rest in the next line.(首先,我想串在第一次分裂/并在下一行的其余部分。) Next, I'd like to surround the first part in a <span> tag, as follows:(接下来,我想在<span>标记中包围第一部分,如下所示:) <div id="date"> <span>23</span> 05/2013</div> 23 05/2013 What I did:(我做了什么:) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="date">23/05/2013</div> <script type="text/javascript"> $(document).ready(function() { $("#date").text().substring(0, 2) + '<br />'; }); </script> See the JSFiddle .(见JSFiddle 。) But this does not work.(但这不起作用。) Can someone help me with jQuery?(有人可以用jQuery帮我吗?)   ask by Mustapha Aoussar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using split()(使用split())

Snippet :(片段:) var data =$('#date').text(); var arr = data.split('/'); $("#date").html("<span>"+arr[0] + "</span></br>" + arr[1]+"/"+arr[2]); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="date">23/05/2013</div> Fiddle(小提琴) When you split this string ---> 23/05/2013 on /(当你拆分这个字符串---> 23/05/2013 on /) var myString = "23/05/2013"; var arr = myString.split('/'); you'll get an array of size 3(你会得到一个3的数组) arr[0] --> 23 arr[1] --> 05 arr[2] --> 2013

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...