2017-07-18 6 views
10


मेरे पास ऐसा HTML कोड है।jQuery लक्ष्य वर्ग अलग से

<!-- Every time string is different --> 
<span class="time_string">123456789</span> 
<span class="time_string">981251100</span> 
<span class="time_string">051036626</span> 
<span class="time_string">016165656</span> 

मैं jQuery के समय स्ट्रिंग को बदलने तिथि करने के लिए

<!-- Look every date is different --> 
<span class="time_string">25 March 2017</span> 
<span class="time_string">28 April 2017</span> 
<span class="time_string">14 January 2017</span> 
<span class="time_string">21 December 2015</span> 

ऐसा करने के लिए मैं का इस्तेमाल किया है इस jQuery कोड हैं:

<script type="text/javascript"> 

    function timestr_to_date(time_string){ 
     // this function will convert the time string to date 
    } 

    $(document).ready(function(){ 

     // getting the time string 
     var time_string = $('.time_string').html(); 

     // converting the time string to date by using function 
     var date = timestr_to_date(time_string); 

     // replacing the time_string to date 
     $('.time_string').html(date); 

    }); 

</script> 

लेकिन यह मुझे मिल गया प्रत्येक तारीख

<!-- PROBLEM: Every date are same --> 
<span class="time_string">25 March 2017</span> 
<span class="time_string">25 March 2017</span> 
<span class="time_string">25 March 2017</span> 
<span class="time_string">25 March 2017</span> 
+0

हम्म .. क्योंकि पर कोई सीमा नहीं है और मुझे नहीं पता कि मेरे पास कितना होगा। मैं उनको उत्पन्न करने के लिए PHP का उपयोग करूंगा। और कोई निश्चित संख्या नहीं होगी – JaTurna

+1

यहां आईडी के लिए कोई आवश्यकता नहीं है। कक्षाएं इसके लिए प्रीफेक्ट हैं क्योंकि यह तत्वों की संख्या को सीमित नहीं करती है, या भविष्य के रखरखाव की आवश्यकता नहीं होती है –

+0

@JaTurna आह मैं देखता हूं कि यह काफी उचित है :) मेरी टिप्पणी को हटा देगा: डी – ThisGuyHasTwoThumbs

उत्तर

13

इसे ठीक करने के लिए आपको प्रत्येक .time_string तत्व के माध्यम से लूप की आवश्यकता है और उनके मूल्यों को अलग-अलग संशोधित करना होगा। आप एक each() पाश का उपयोग करने वाले सकता है, या अधिक संक्षेप, जो वर्तमान मूल्य लेता है और मूल्य आप के लिए अद्यतन करना चाहते रिटर्न text() करने के लिए एक समारोह प्रदान करके

ऐसा करने के लिए। इस प्रयास करें:

$('.time_string').text(function(i, t) { 
 
    return timestr_to_date(t); 
 
}); 
 

 
function timestr_to_date(time_string) { 
 
    // your date formatting logic here, this is just for this example: 
 
    return new Date(time_string * 1000); 
 
}
span { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span class="time_string">123456789</span> 
 
<span class="time_string">981251100</span> 
 
<span class="time_string">051036626</span> 
 
<span class="time_string">016165656</span>

3

आप सभी वर्गों के माध्यम से लूप चाहिए:

$(document).ready(function(){ 


    // getting the time string 
    $('.time_string').each(function(){ 
     var time_string = $(this).text(); 

     // converting the time string to date by using function 
     var date = timestr_to_date(time_string); 

     // replacing the time_string to date 
     $(this).text(date); 
     }); 

    }); 
3

दोहराएं सभी span के माध्यम से वर्ग time_string साथ और उनके मान बदलने के नीचे खोजने के लिए और अधिक जानकारी के लिए स्निपेट

कृपया

function timestr_to_date(time_string) { 
 
    // this function will convert the time string to date 
 
} 
 

 
$(document).ready(function() { 
 
    $(".time_string").each(function() { 
 
    // getting the time string 
 
    var time_string = $(this).html(); 
 
    // converting the time string to date by using function 
 
    var date = timestr_to_date(time_string); 
 
    // replacing the time_string to date 
 
    $(this).html(date); 
 
    }); 
 
});
<span class="time_string">123456789</span> 
 
<span class="time_string">981251100</span> 
 
<span class="time_string">051036626</span> 
 
<span class="time_string">016165656</span>

6

आप का उपयोग करना चाहिए jQuery.eachhttps://api.jquery.com/each/

$('.time_string').each(function() { 
    var time_string = $(this).html(); 

    var date = timestr_to_date(time_string); 

    $(this).html(date); 
}); 
2

आप सभी अवधि टैग में पाश बनाना चाहिए, इस कोड का प्रयास करें:

$(document).ready(function(){ 

    $("span").each(function() { 

     // getting the time string 
     var time_string = $(this).html(); 

     // converting the time string to date by using function 
     var date = timestr_to_date(time_string); 

     // replacing the time_string to date 
     $(this).html(date); 

    }); 

}); 
2

आप सभी अवधि टैग पुनरावृति के लिए होनी चाहिए और परिवर्तित

$('span.time_string').each(function(){ 
    var time = $(this).text();  
    var date = timestr_to_date(time); 
    $(this).text(date); 
}); 
की तरह तिथि में टाइमस्टैम्प 10
संबंधित मुद्दे