gpt4 book ai didi

javascript - jQuery 不更改动态创建图像的图像源属性

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:10 24 4
gpt4 key购买 nike

我在点击我的页面时动态创建元素:

                <img
src="/imagecache/large/{{ $issue->first()->image }}"
onclick="magazineDetail(
'{{ $issue->first()->magazine->id }}',
'{{ $issue->first()->magazine->name }}',
'{{ $issue->first()->magazine->summary ?: '' }}',
'{{ $issue->first()->magazine->image ?: '' }}',
'{{ $issue->first()->image }}'
)"
>

我通过点击调用这个脚本:

function magazineDetail(id, name, summary, issueImage, magazineImage){
images = [];
nextImage = 0;
loadedImages = [];

$('#magazine-detail')[0].innerHTML = '<section id="magazine-detail" class="magazine-detail"><div class="large-6 medium-6 small-12 columns"><div class="magazine-hero"><img id="image" src="/imagecache/cover/' + magazineImage + '" alt="' + name + '" /><div class="magazine-preview-nav"><div class="right-arrow" id="forward"><img src="/img/right-arrow-black.svg" /><p>Neste utgivelse</p></div><div class="left-arrow" id="back"><img src="/img/left-arrow-black.svg" /><p>Forrige utgivelse</p></div></div></div></div><div class="large-6 medium-6 small-12 columns"><div class="row"><div class="small-6 columns magazine-title"><h1 id="name"></h1></div></div><p id="summary"></p><img id="issueImage" src="" alt="" /><p></p><button class="button primary expand">Kjøp abonnement - 1 måned gratis</button><button class="button secondary expand">Bla igjennom arkivet</button></div></section>';

$('#image').attr({"src" : '/imagecache/cover/' + magazineImage, "alt" : name});
$('#name').text(name);
$('#summary').text(summary);

if (issueImage != '') {
$('#issueImage').html('<img src="/imagecache/medium/"' + issueImage + ' alt="' + name + '">');
}

$('html, body').animate({
scrollTop: $("#magazine-detail").offset().top + 1500
}, 1700);

$.getJSON("issue/images",
{ id: id },
function(result){
if (result.length < 2){
$('.magazine-preview-nav').hide();
} else {
$('.magazine-preview-nav').show();
}
$.each(result, function(i, value){
images.push(value);
});
function imagePreload() {
preload();
};
});

console.log(images);
}

我有<div class="magazine-hero"><img id="image" src="/imagecache/cover/' + magazineImage + '" alt="' + name + '" />我正在创建 $('#magazine-detail')[0].innerHTML .

然后我有一个函数应该用 id="image" 改变那个元素的 src单击时正在创建。这是负责处理的脚本:

$(document).ready(function () {
imagesIndex = 0;
nextImage = 0;
loadedImages = new Array();

function preload() {
console.log('entered');
for (i = 0; i < 2; i++) {
if (nextImage < images.length) {
var img = new Image();
img.src = '/imagecache/cover/' + images[nextImage];
loadedImages[nextImage] = img;
++nextImage;
}
}
}

$('#magazine-detail').on('click','#forward', function() {
imagesIndex++;
preload();

if (imagesIndex > (loadedImages.length - 1)) {
imagesIndex = loadedImages.length - 1;
}
console.log(loadedImages.length);
console.log(loadedImages[imagesIndex].src);
$('#image').attr({"src" : loadedImages[imagesIndex].src, "alt" : name});
});

$('#magazine-detail').on('click','#forward', function() {
imagesIndex--;

if (imagesIndex < 0) {
imagesIndex = 0;
}

$('#image').attr({"src" : loadedImages[imagesIndex].src, "alt" : name});
});
});

关于检查 console.logs我看到正在使用 preload function 创建新图像, 以及正确的 image src正在传递给: $('#image').attr({"src" : loadedImages[imagesIndex].src, "alt" : name});

但是页面上的图像没有改变。我也许还应该提到,当我没有在页面加载后创建这些元素但将它们隐藏时,一切正常。

最佳答案

$(document).ready(function(){ }); 似乎有一个小错误。

该代码对同一个按钮 #forward 有 2 个点击事件。可能应该将一个分配给 #back 按钮。

关于javascript - jQuery 不更改动态创建图像的图像源属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40240341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com