String elementini ajratib bo'lmaydigan va <div> ga qo'shib bo'lmadi

Funtionallik:

Menda A, 4.0,00: 04 kodi bor. Stringni ajratishning asosiy maqsadi 00:04 qiymatini quyidagi "min" va "soniya" divlariga qo'shib qo'yishdir, shuning uchun men yana 00:04 kabi qiymatni qo'shsangiz, u "00 min" "04 sek" sifatida ko'rsatiladi.

Muammo:

Shu nuqtada "," dan ajratish imkoniyatiga ega emasman va Split magistrida myconsole.log faylini o'qiganimda 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.

Iltimos yordam bering.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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>
</div> </div>
0
To'g'ridan-to'g'ri, Miya muzqaymoqi. Tushundim
qo'shib qo'ydi muallif Luke, manba

6 javoblar

RegExp / bilan String.prototype.match() dan foydalanishingiz mumkin. ["00", "04"] ni o'z ichiga olgan qatorni olish uchun ":" yoki qator oxiridagi raqamlarni kiriting.

Use multiple selectors at jQuery() to make single call to .html(function)

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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> </div>
2
qo'shib qo'ydi
@Phil g bayrog'ini nazarda tutasizmi?
qo'shib qo'ydi muallif guest271314, manba
@Phil Bir OR | mavjud. \ d + $ faqat satr oxirida raqamlarga mos keladi. g holda, faqat \ d {2} (? =:) ga mos keladi. Birinchi kodni \ d + (? =:) ga yangilab, ikkita belgi saqlaydi va bir xil natijaga qaytishi kerak.
qo'shib qo'ydi muallif guest271314, manba
$/g ? Siz ko'p satrli satrlarni kutmoqdasiz? BTW, bu .html (fn) narsa juda sekin
qo'shib qo'ydi muallif Phil, manba
Ha, oxirigacha chizilgan va global bayroq bor ekan, barchasi bir g'alati tuyuladi
qo'shib qo'ydi muallif Phil, manba
Eh, ha, miyam boshqa lahzani ajratib qo'ydi. Yaxshi :)
qo'shib qo'ydi muallif Phil, manba

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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>
</div> </div>

Split again the data using ":"

1
qo'shib qo'ydi

Quyidagi kabi harakat qila olasizmi?

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

$("#Game_BestTimer_Minute").html(value[0]);
$("#Game_BestTimer_Second").html(value[1]);
1
qo'shib qo'ydi

Jadval oxirida \ d +: \ d + ni qidirish uchun muntazam ifodani ishlatish uchun ko'proq mos keladi ...

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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>
</div> </div>
1
qo'shib qo'ydi
match kerak bo'lmasa var [ min, sek] = data.match (/ (\ d +): (\ d +) $ /)
qo'shib qo'ydi muallif guest271314, manba
@ guest271314 cheers, faqatgina 3 ta natijani ko'rganini payqashni istardi
qo'shib qo'ydi muallif Phil, manba

Faqatgina .split() deb chaqirib, cheklovchi sifatida vergul o'rniga bitta nuqta yordamida foydalaning.

Shuningdek, qadriyatlaringizni DOMga kiritish uchun .text() dan .html() dan foydalanish ancha tezroq ekanligini unutmang.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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]);
<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>
</div> </div>
1
qo'shib qo'ydi

Ajoyib, lekin ajablanyapman, nega siz yana ajralib ketmaysiz. Maydoni olib tashlash uchun chaqiruvni eslab qoling.

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

$ ("# Game_BestTimer_Minute"). Html (timerList [0] .trim ());   $ ("# Game_BestTimer_Second"). Html (timerList [1] .trim ());

1
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb