2012-12-14 49 views
3

這是我的問題。我在我的網站上創建自己的圖像彈出窗口。

這是彈出

<script> clicked = $('#gatb-1').click(function() { 
    return this.getAttribute('imid') }); </script> 

*注前的腳本:點擊是全局變量

這是圖片代碼:

<?php foreach ($the_picture as $picture) : ?> 
<a id="gatb-1" imid="http://somesite.com/img/image.jpg" class="glr"> 
<div class="picture"> 
<img src="http://somesite.com/img/image.jpg" width="340px"> 
</div> 
</a> 
<?php endforeach; ?> 

然後彈出代碼下面的圖片代碼被稱爲,這是彈出顯示時的腳本:block

這是彈出DIV:

<div class="popup"> 
<script> 
$(document).ready(function() { 
    $('#thmg').attr('src', clicked); }); 
</script> 
<table> 
<tr> 
<td>left link</td> 
<td><div class="picts"><img id="thmg" src="target here"></div></td> 
<td>Right link</td> 
</table> 
</div> 

我還曾試圖彈出在此之前的代碼,但它是相同的。總是返回[對象的對象]或返回null

$(this).attr('imid') 

的結果是這樣的:

<img src="[object object]"> 

這樣,我怎麼能傳遞屬性?或者我做錯了什麼?

+0

除了創建無效的HTML嗎? –

+3

這沒有任何意義,在錨中有一個img屬性,你不關閉函數,你基本上用無效的HTML做所有事情都是錯的,奇怪的javascript將img屬性返回給全局的方式)等 – adeneo

回答

0

我已經找到了!它不是關於錯誤的代碼或任何東西。其僅僅因爲<script></script>是全球不同的。其因爲我在1個文件的兩個地方有<script></script>

<script> 
clicked = $('#gatb-1').click(function() { 
    return this.getAttribute('imid') }); 
</script> 

然後訪問另一個變量<script></script>是讓它不工作的那個。

<script> 
$(document).ready(function() { 
     $('#thmg').attr('src', clicked); 
}); 
</script> 

所以,解決的辦法是,把所有的jQuery整合到一個<script></script>中。

<script> 
$(document).ready(function(){ 
    $('#gatb-1').click(function(){ 
    var clicked = $(this).attr('imid'); 
    $('#thmg').attr('src', clicked); 
}); 
}); 
</script> 
0

作品對我來說...

var img = $("#gatb-1").attr('img'); 
$("#thmg").attr('src', img); 

http://jsfiddle.net/chovy/Srx55/

+0

謝謝。但仍然我有一個[對象對象]值在src =「」是因爲圖像上的循環? – faiz

0

我想你分配到$('#gatb-1')代替clicked的是IMG屬性。如果你要使用該代碼(我不鼓勵這樣做),$('#thmg').attr('src', clicked.attr('img'))可能會工作

+0

謝謝,但$('#thmg')。attr('src',點擊。attr('img'))'必須傳遞給哪個變量? – faiz

+0

該代碼會改變彈出的圖像 – atnatn

0

嘗試更換

<script> clicked = $('#gatb-1').click(function() { 
return this.getAttribute('img') }); </script> 

<script> $('#gatb-1').click(function() { 
clicked = $(this).attr('img') }); </script> 
+0

我試圖使用這種風格,但它給了我一個像這樣的空白輸出 faiz

相關問題