1

我在页面上有许多具有相同 ID 的元素。这里有两个:

<span id="worldwide">United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy</span>

<span id="worldwide">United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy</span>

每个元素要么包含所有国家/地区,要么仅包含一个国家/地区。示例中的这两个都与许多国家/地区有关。我希望 jquery 检查每个长度是否超过 100,如果它真的用一个词替换所有国家 - WORLDWIDE。

这就是我使用的:

<script>
    $("#worldwide").each(function () {
        if ($("#worldwide").text().length > 100) {
            $("#worldwide").text('WORLDWIDE')
        }
    })
</script>

这仅适用于更改第一个 div,但不会触及第二个。我相信我没有很好地使用 .each 功能请让我知道可能出了什么问题

4

6 回答 6

3

问题是您拥有具有相同id属性的元素,而元素应该具有唯一id的 s。放置类而不是相等的id属性,例如

<span class="worldwide">...</span>

并使用此代码:

$(".worldwide").each(function() {
    if ($(this).text().length > 100) {
        $(this).text('WORLDWIDE');
    }
});

演示:http: //jsfiddle.net/aD7HQ/

于 2012-05-21T20:49:28.227 回答
1

ID 是唯一的标识符。如果您有一堆想要表现相似的元素,请使用class而不是id.

于 2012-05-21T20:49:44.297 回答
1

任何 ID 的实例都不能超过一个,这就是它被称为 ID 的原因。

你最好改用类。

另外,请注意 JQuery 要求在每条语句的末尾使用分号。

<span class="worldwide">United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy</span>

<span class="worldwide">United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy United Kingdom Australia United States Lithuania Russia Spain France Portugal Italy</span>


<script>

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


if ($(this).text().length > 100) { $(this).text('WORLDWIDE'); }

});
</script>
于 2012-05-21T20:50:32.670 回答
1

我会使用这种.text()方法。

$(".worldwide").text(function(i,text){
    return text.length > 100 ? 'WORLDWIDE' : text;
});

此外,明显的 ID 必须是唯一的。如果您不能使您的 id 唯一或使用类来代替,这是一种低效的解决方法。

$("[id=worldwide]").text(...
于 2012-05-21T20:56:05.557 回答
1

您不应使用具有相同 ID 的多个元素。如果在你的情况下是绝对必要的,你可以使用这个:

$('span').each(function(index,element){
    if(element.id == 'worldwide'){
        //Do Something
    }
});
于 2012-05-21T20:54:32.590 回答
0

首先,您不能重复 ID,这违反了 Web 标准,并且浏览器无法分辨出您所引用的全球范围内的内容,只需将它们更改为 class 即可:

<span class="worldwide">...</span>
<span class="worldwide">...</span>

每个元素要么包含所有国家/地区,要么仅包含一个国家/地区。示例中的这两个都与许多国家/地区有关。我希望 jquery 检查每个长度是否超过 100,如果它真的用一个词替换所有国家 - 全球。

这就是我使用的:

<script>
    $(".worldwide").each(function() {
        if ($(this).text().length > 100) { $(this).text('WORLDWIDE')}
    })
</script>

注意函数内部的$(this) 。

于 2012-05-21T20:50:44.513 回答