2014-08-30 106 views
0

我在jQuery中更改src属性时遇到问题。jquery attr(src)第一次不工作

这是一些经验:

<a href='Imagini/someimage.jpg' class='popup' data-id='n' > 
    <img src='Imagini-min/someimage.jpg' /> 
</a> 

这里,弹出图像的容器:

<div class="gallery" align="center"> 
    <p id="close"><img src="Resurse/close.png" /></p> 
    <p id="prev"><img src="Resurse/left.png" /></p> 
     <img id="change" src="" data-id=""/> 
    <p id="next"><img src="Resurse/right.png" /></p> 
</div> 

和jQuery代码:

$(".popup").click(function(e){ 
    e.preventDefault(); 

    var img = $(this).attr("href"); 
    var id = $(this).attr("data-id"); 

    $(".gallery").show(); 
    if(id != $(".gallery").find("#change").attr("data-id")){ 
     $(".gallery").find("#change").attr("src", img).hide().fadeIn(100); 
     $(".gallery").find("#change").attr("data-id", id); 

     SetPosition(); 
    } 
    }); 

function SetPosition(){ 
    var width = $(".gallery").find("#change").width(); 
    var height = $(".gallery").find("#change").height(); 
    var button_h = $(".gallery #prev img").height(); 

    $(".gallery #prev").css("top",(height/2 - button_h + 10)); 
    $(".gallery #next").css("top",(height/2 - button_h + 8)); 

    $(".gallery").css("width",(width+50)); 
    $(".gallery").css("height",(height+10)); 
} 

的问题是,当我打开浏览器并单击我得到的任何图像: http://oi60.tinypic.com/2mq5gmg.jpg

如果按一次,去正常: http://oi59.tinypic.com/qpqdk8.jpg

有人有这样的问题?

+0

被包裹在一个文档准备事件处理程序的代码? – melancia 2014-08-30 13:01:54

+0

什么意思是“打开浏览器并点击任何图像”和“如果按一次,正常”? – 2014-08-30 13:02:16

+0

@MelanciaUK是的。 – vTudor 2014-08-30 13:03:41

回答

0

看看这个:

change src

HTML

<a href='javascript:void(0)' src='Imagini/someimage.jpg' class='imagePopup' data-id='n' > 
    <img src='Imagini-min/someimage.jpg' /> 
</a> 
<div class="gallery" align="center"> 
    <p id="close"><img src="Resurse/close.png" /></p> 
    <p id="prev"><img src="Resurse/left.png" /></p> 
     <img id="change" src="" data-id=""/> 
    <p id="next"><img src="Resurse/right.png" /></p> 
</div> 

jQuery的

$("a.imagePopup").click(function(e){ 
    //The event.preventDefault() method stops the default action of an element from happening. but it does not work in all browser. 

    e.preventDefault(); 

    var img = $(this).attr("src"); 
    var id = $(this).attr("data-id"); 
    var galleryID=$(".gallery").find("#change").attr("data-id"); 
    var targetImg=$(".gallery").find("#change"); 
    $(".gallery").show();  
    if(id != targetImg.attr("data-id")){ 
     targetImg.attr("src", img).hide().fadeIn(100).attr("data-id", id); 
     alert("src="+targetImg.attr("src")+", id="+targetImg.attr("data-id")); 

     SetPosition(); 
    } 
    }); 

function SetPosition(){ 
    var width = $(".gallery").find("#change").width(); 
    var height = $(".gallery").find("#change").height(); 
    var button_h = $(".gallery #prev img").height(); 

    $(".gallery #prev").css("top",(height/2 - button_h + 10)); 
    $(".gallery #next").css("top",(height/2 - button_h + 8)); 

    $(".gallery").css("width",(width+50)); 
    $(".gallery").css("height",(height+10)); 
} 

更新

在img.load()中调用“SetPosition”函数; Update

+0

同样存在问题,请检查http://sitefortest.url.ph/articole.php?art=53中的图像以查看错误。我认为,jquery工作缓慢。 – vTudor 2014-08-30 13:50:52

+0

vTudor查看更新。 – Hamix 2014-08-30 14:04:08

+0

非常感谢,以这种方式快速充电!编辑你的文章,或我现在会给你正确的答案? – vTudor 2014-08-30 14:20:49

0

也许这种变化有助于:

$(".gallery").find("#change").hide(); 
$(".gallery").find("#change").attr("src", img); 
$(".gallery").find("#change").fadeIn(100);