无法拆分字符串元素并追加到< div> [英] Unable to split String element and append to <div>
问题描述
00:04
的值附加到下面的min和secdiv,因此,我需要进一步分割 00:04
的值,这样当我追加值时,它将显示为00分钟04秒。 问题:
此时我无法进一步分割 当我在Split字符串上执行我的console.log时,它将返回A |的单个String元素4.0 | 00:04。
然而,在这一点上,我不知道如何进一步去取出00和04这个单独的元素来追加到分钟& secdiv标记。
请帮助。
< script src =https://ajax.googleapis.com/ajax/libs /jquery/2.1.1/jquery.min.js\"></script><div id =Game_BestTimer_Minutestyle =font-family:'Avenir';>< / div>< div
您可以使用 String.prototype.match()
和 RegExp
/ \d +(?= :) | \d + $ / g
匹配一个或多个数字字符,后跟:
或字符串结尾的数字来获取包含 [00,04]
的数组。
在 jQuery()
中使用多个选择器对 .html(function)
var data ='A,4.0,00:04'; var TimerList = data.match(/ \d +(?= :) | \d + $ / g); console.log(TimerList); $(#Game_BestTimer_Minute,#Game_BestTimer_Second)。html(function(index,_){return TimerList [index]});
$ p $< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1。
Funtionality:
I have a String A,4.0,00:04
. The main purpose to Split the String is to append the value of 00:04
to the following div of "min" and "sec", hence, i will need to further split the value of 00:04
such that when i append the value it will be displayed as "00 min" "04 sec".
Issue:
At this point I am unable to to split further than the "," and when i do my console.log on the Split string, it will return the individual String element of A| 4.0| 00:04.
However, at this point, I do not know how to proceed further to take out the individual element of "00" and "04" to append to the "min" & "sec" div tag.
Please help.
var data = 'A,4.0,00:04';
console.log("BestTime: " + data);
//When I consolelog BESTTIME, it will return the element of (A, 4.0, 00:04)
var TimerList = data.split(",");
console.log("TimerList: " + TimerList[0] + "|" + TimerList[1] + "|" + TimerList[2]);
//TimerList[0] => A //Not What I want to append to div tag
//TimerList[1] => 4.0 // Not What I want to append to div tag
//TimerList[2] => 00:04 //Need to futher split this so that I can append 00 to Game_BestTimer_Minute & 04 to Game_BestTimer_Second
//append BEST GAME TIME to SCOREBOARD
$("#Game_BestTimer_Minute").html(TimerList[2]);
$("#Game_BestTimer_Second").html(TimerList[2]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Game_BestTimer_Minute" style="font-family:'Avenir';"></div>
<div id="Game_BestTimer_Second" style="font-family:'Avenir';"></div>
You can use String.prototype.match()
with RegExp
/\d+(?=:)|\d+$/g
to match one or more digit characters followed by ":"
or digits at end of string to get array containing ["00", "04"]
.
Use multiple selectors at jQuery()
to make single call to .html(function)
var data = 'A,4.0,00:04';
var TimerList = data.match(/\d+(?=:)|\d+$/g);
console.log(TimerList);
$("#Game_BestTimer_Minute, #Game_BestTimer_Second")
.html(function(index, _) {
return TimerList[index]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div id="Game_BestTimer_Minute" style="font-family:'Avenir';"></div>
<div id="Game_BestTimer_Second" style="font-family:'Avenir';"></div>
这篇关于无法拆分字符串元素并追加到< div>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!