Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery分别以类为目标_Jquery_Html - Fatal编程技术网

jQuery分别以类为目标

jQuery分别以类为目标,jquery,html,Jquery,Html,我有这样的HTML代码。 <!-- 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_stri


我有这样的HTML代码。

<!-- 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>

123456789
981251100
051036626
016165656
我希望Jquery将时间字符串更改为date

<!-- 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>
<!-- 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>

2017年3月25日
2017年4月28日
2017年1月14日
2015年12月21日
为此,我使用了以下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>

函数timestr_to_date(时间字符串){
//此函数将时间字符串转换为日期
}
$(文档).ready(函数(){
//获取时间字符串
var time_string=$('.time_string').html();
//使用函数将时间字符串转换为日期
var date=timestr_至_日期(时间字符串);
//将时间字符串替换为日期
$('.time_string').html(日期);
});
但我每次约会都一样

<!-- 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>
<!-- 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>

2017年3月25日
2017年3月25日
2017年3月25日
2017年3月25日

要解决此问题,您需要遍历每个
.time\u字符串
元素,并分别修改它们的值

为此,您可以使用
each()
循环,或者更简洁地说,通过向
text()
提供一个函数,该函数获取当前值并返回要更新的值。试试这个:

$('.time_string').text(函数(i,t){
返回时间TR_至_日期(t);
});
函数timestr_to_date(时间字符串){
//此处是您的日期格式逻辑,仅适用于此示例:
返回新日期(时间字符串*1000);
}
span{display:block;}

123456789
981251100
051036626

016165656
您应该循环浏览所有类:

$(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);
       });

    });

使用class
time\u string
遍历所有
span
,并更改其值。有关详细信息,请查找下面的代码段

函数timestr到日期(时间字符串){
//此函数将时间字符串转换为日期
}
$(文档).ready(函数(){
$(“.time_字符串”).each(函数(){
//获取时间字符串
var time_string=$(this.html();
//使用函数将时间字符串转换为日期
var date=timestr_至_日期(时间字符串);
//将时间字符串替换为日期
$(this).html(日期);
});
});
123456789
981251100
051036626

016165656
您应该使用
jQuery.each


您应该在所有span标记中创建循环,请尝试以下代码:

$(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);

   });

});

您应该迭代所有span标记,并将时间戳转换为如下所示的日期

$('span.time_string').each(function(){
     var time = $(this).text();      
     var date = timestr_to_date(time);
     $(this).text(date);
});

隐马尔可夫模型。。因为没有限制,我不知道我会有多少。我将使用PHP生成这些。这里没有固定号码,根本不需要身份证。类在这方面是完美的,因为它不限制元素的数量,也不需要任何未来的元素maintenance@JaTurna啊,我看这很公平:)将删除我的评论:D