无法拆分String元素并追加到<div>

时间:2017-03-07 02:51:57

标签: javascript jquery html

Funtionality:

我有一个字符串A,4.0,00:04。拆分字符串的主要目的是将00:04的值附加到&#34; min&#34;的下一个div中。和&#34; sec&#34;因此,我需要进一步分割00:04的值,这样当我附加值时,它将显示为&#34; 00 min&#34; &#34; 04秒&#34;。

问题:

此时我无法进一步分裂&#34;,&#34;当我在Split字符串上执行我的console.log时,它将返回A |的单个String元素4.0 | 00:04。

然而,在这一点上,我不知道如何进一步取出&#34; 00&#34;和&#34; 04&#34;附加到&#34; min&#34; &安培; &#34;秒&#34; div标签。

请帮忙。

&#13;
&#13;
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]);
&#13;
<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>
&#13;
&#13;
&#13;

6 个答案:

答案 0 :(得分:2)

您可以将String.prototype.match()RegExp /\d+(?=:)|\d+$/g匹配,以匹配一个或多个数字字符,后跟":"或字符串末尾的数字,以获取包含{{1}的数组}。

使用["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]
});

答案 1 :(得分:1)

var data1 = "A,4.0,00:04";
var data = data1.split(",")[2]
var min = data.split(":")[0]
var sec = data.split(":")[1]
console.log("BestTime: " + data );

//var TimerList = data.split(",");
console.log("TimerList: " + data + "|" + min + "|" + sec);

//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(min);
$("#Game_BestTimer_Second").html(sec);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Game_BestTimer_Minute" style="z-index:10; position:absolute; top:450px; left:870px; font-size:160px; font-family:'Avenir'; width:1080px; color:#fff;"></div>

<div id="Game_BestTimer_Second" style="z-index:10; position:absolute; top:450px; left:1215px; font-size:160px; font-family:'Avenir'; width:1080px; color:#fff;"></div>

使用":"

再次拆分数据

答案 2 :(得分:1)

你能尝试如下吗?

var value = TimerList[2].split(':');

$("#Game_BestTimer_Minute").html(value[0]);
$("#Game_BestTimer_Second").html(value[1]);

答案 3 :(得分:1)

很好,但我想知道你为什么不再分裂。记住调用修剪以移除空间。

var TimerList = data.split(",");
console.log("TimerList: " + TimerList[0] + "|" + TimerList[1] + "|" +     TimerList[2]);
var timerList = TimerList[2].split(":");
//append BEST GAME TIME to SCOREBOARD

$(&#34;#Game_BestTimer_Minute&#34)。HTML(timerList [0] .trim());   $(&#34;#Game_BestTimer_Second&#34)。HTML(timerList [1] .trim());

答案 4 :(得分:1)

再次使用冒号而不是逗号作为分隔符再次调用.split()

另请注意,使用.text().html()将值插入DOM要快得多。

&#13;
&#13;
var data = "A,4.0,00:04"

console.log("BestTime: " + data);

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

var MinSec = TimerList[2].split(":");

//append BEST GAME TIME to SCOREBOARD
$("#Game_BestTimer_Minute").text(MinSec[0]);
$("#Game_BestTimer_Second").text(MinSec[1]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="Game_BestTimer_Minute"></div>
<div id="Game_BestTimer_Second"></div>
&#13;
&#13;
&#13;

答案 5 :(得分:1)

似乎更适合使用正则表达式在字符串末尾查找\d+:\d+ ...

var data = 'A,4.0,00:04';
console.log("BestTime: " + data);

var [match, min, sec] = data.match(/(\d+):(\d+)$/)

$("#Game_BestTimer_Minute").html(min);
$("#Game_BestTimer_Second").html(sec);
<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>