我在页面上有许多具有相同 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 功能请让我知道可能出了什么问题