Javascript Can';t使用jquery选择已更改的图像src
我已经通过jquery更改了img src,但现在我想在单击image时打开一个模式,由于某些原因,我无法选择更改的img,当我单击它时,操作将转到src更改之前的映像。 这是我的密码: HTML: 因此,每当我切换到第二个图像并单击顶部时,它会发送警报“123”,而不是我想要的“234”。你可以这样做Javascript Can';t使用jquery选择已更改的图像src,javascript,html,jquery,Javascript,Html,Jquery,我已经通过jquery更改了img src,但现在我想在单击image时打开一个模式,由于某些原因,我无法选择更改的img,当我单击它时,操作将转到src更改之前的映像。 这是我的密码: HTML: 因此,每当我切换到第二个图像并单击顶部时,它会发送警报“123”,而不是我想要的“234”。你可以这样做 $('#imagemplanta')。单击(函数() { 让imgSrc=$(this.attr('src')) ,imgIdx=imgSrc.lastIndexOf(“/”)+1 ,imgNa
$('#imagemplanta')。单击(函数()
{
让imgSrc=$(this.attr('src'))
,imgIdx=imgSrc.lastIndexOf(“/”)+1
,imgName=imgSrc.substr(imgIdx)
;
如果(imgName=='planta1.jpg')警报('123')
else警报('234')
})
当页面加载时,点击分配就完成了,它无法动态工作。解决方法是什么?非常感谢
<div class="tipoplantas text-center">
<button id="botao2" type="button" class="botaoplantas">1</button>
<button id="botao1" type="button" class="botaoplantas">2</button>
</div>
<div class="imagemplanta text-center">
<img id="imagemplanta" src="img/infinity/planta1.jpg" class="img-fluid">
</div>
// Switch Images
$('#botao1').click(function(){
$('#imagemplanta').each(function(){
$(this).animate({
opacity: 0
}, 500, function(){
$('#botao1').addClass('selected')
$('#botao2').removeClass('selected')
$(this).attr('src', '/img/infinity/planta1.jpg');
});
$(this).animate({
opacity: 1
}, 500);
});
});
$('#botao2').click(function(){
$('#imagemplanta').each(function(){
$(this).animate({
opacity: 0
}, 500, function(){
$('#botao2').addClass('selected')
$('#botao1').removeClass('selected')
$(this).attr('src', '/img/infinity/planta2.jpg');
});
$(this).animate({
opacity: 1
}, 500);
});
});
// Test
$("img[src$='img/infinity/planta2.jpg']").click(function() {
alert('234');
})
$("img[src$='img/infinity/planta1.jpg']").click(function() {
alert('123');
})