【问题标题】:Getting text from <li>´s & check duplicates & .append it... by jQuery从 <li> 获取文本 & 检查重复 & .append it... by jQuery
【发布时间】:2012-05-15 11:37:34
【问题描述】:

HTML:

<li>Tue, 15th May 2012 10:11</li>
<li>Tue, 15th May 2012 10:12</li>
<li>Tue, 15th May 2012 10:13</li>
<li>Tue, 15th May 2012 10:14</li>
<li>Tue, 15th May 2012 10:15</li>
<li>Tue, 16th May 2012 21:08</li>
<li>Tue, 16th May 2012 21:07</li>
<li>Tue, 16th May 2012 21:06</li>
<code></code>

jQuery :

$("li").each(function () {
    words = $(this).text().split(' ');
    xxx = words[1]+" "+words[2]+" "+words[3]+",";
    $("code").append(xxx); 
});

我现在来到这一步,但我不知道如何检查重复日期并附加唯一日期

演示:http://jsfiddle.net/4kvUy/

【问题讨论】:

    标签: jquery text get duplicates


    【解决方案1】:

    这样做:-

    if ($("code:contains('"+xxx+"')").length == 0)
       $("code").append(xxx);
    

    编辑:

    要处理逗号,请尝试以下方式:-

    if ($("code:contains('"+xxx+"')").length == 0){
        if ($("code").text() == '')
            $("code").append(xxx);
        else
            $("code").append(","+xxx);
    }
    

    编辑2:

    要处理这种类型的输出,请尝试以下方式:-

    $("li").each(function () {
        words = $(this).text().split(' ');
        xxx = words[1]+" "+words[2]+" "+words[3];
        if ($("code:contains('"+xxx+"')").length == 0){     
            if ($("code").text() == '')         
                $("code").append('["'+xxx);     
            else         
                $("code").append('","'+xxx); 
        }
    });
    $("code").append('"]');
    

    输出:

    ["15th May 2012","16th May 2012"]
    

    【讨论】:

    • 如果我需要这样输出的话:["15th May 2012","10th May 2012"]
    【解决方案2】:

    一种选择是使用:contains 选择器。

    $("li").each(function() {
        words = $(this).text().split(' ');
        xxx = words[1] + " " + words[2] + " " + words[3] + ",";
        $("code:not(:contains(" + xxx + "))").append(xxx);
    });​
    

    演示: http://jsfiddle.net/4kvUy/2/

    【讨论】:

      【解决方案3】:

      试试this on jsfiddle,好像行得通。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-10
        • 2021-07-25
        • 2023-02-16
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2012-10-10
        • 2018-01-19
        相关资源
        最近更新 更多