У меня есть ряд URL-адресов, и мне интересно, есть ли способ динамически изменить последнюю цифру.
<div class="m_item">
<a class="thumbnail_link" href="http://paraboladesignstudio.ipage.com/yahaira/fashion/fashion-slideshow/?thumb=0">
<img src="<?php echo $image['sizes']['thumbnail'];?>" title="<?php echo $image['title'];?>" alt="<?php echo $image['alt']; ?>">
</a>
</div>
Вышеприведенный код выводит определенное количество ".m_item" с теми же "a href" s.
Это мой код jQuery:
var i=0;
i++;
$(".thumbnail_link").each(function() {
this.href = this.href.replace("0", i);
});
Он изменил все URL-адреса на "..../? Thumb = 1"
Как я могу увеличить цифру? Я пробовал. Детей без везения.
Спасибо.
Избавьтесь от я и просто используйте индекс каждого() https://api.jquery.com/each/
$(".thumbnail_link").each(function(index) {
this.href = this.href.replace("0", index);
});
Или вы можете сделать это, если в URL-адресе есть другие 0
$(".thumbnail_link").each(function(index) {
this.href = this.href.replace("thumb=0", "thumb=" + index);
});
Он делает это, потому что i
только когда-либо увеличивал один раз. Он начинается как ноль, затем вы ударяете его до 1 с помощью i++
, а затем вы никогда не меняете его снова. Попробуй это:
$(".thumbnail_link").each(function(i) {
this.href = "http://paraboladesignstudio.ipage.com/yahaira/fashion/fashion-slideshow/?thumb=" + i;
});
или если вы хотите обновить последний номер, используйте это
$(".thumbnail_link").each(function(index) {
this.href = this.href.replace(/([\d]+)$/g, index);
});
$(".thumbnail_link").each(function(i) { this.href = this.href.replace("0", i); });
и удалите свою декларациюi