2017-04-26 179 views
-3

我的網站目前使用:你如何做一個for循環中的「href」鏈接?

for (var i = 1; i<25; i++){ 
$('#photos').append('<img src="Sections/Blondes/'+i+'.jpg">'); 
} 

顯示圖像,但是當我做同樣的事情的 「href」:

$("#photos").append("href", "http://www.google.com/") 

這裏是我的CSS 「#photos」:

#photos img { 
    width: 100% ; 
    height: auto; 
} 
@media only screen and (min-device-width: 1080px) { 
#photos { 
    line-height: 0; 
    -webkit-column-count: 5; 
    -webkit-column-gap: 0px; 
    -moz-column-count: 5; 
    -moz-column-gap:  0px; 
    column-count:   5; 
    column-gap:   0px; 
} 
#photos img { 
    width: 100% ; 
    height: auto; 
} 
@media (max-width: 1200px) { 
    #photos { 
    -moz-column-count: 4; 
    -webkit-column-count: 4; 
    column-count:   4; 
    } 
} 
@media (max-width: 1000px) { 
    #photos { 
    -moz-column-count: 3; 
    -webkit-column-count: 3; 
    column-count:   3; 
    } 
} 
@media (max-width: 800px) { 
    #photos { 
    -moz-column-count: 2; 
    -webkit-column-count: 2; 
    column-count:   2; 
    } 
} 
@media (max-width: 400px) { 
    #photos { 
    -moz-column-count: 1; 
    -webkit-column-count: 1; 
    column-count:   1; 
    } 
} 
} 

我想爲每張圖片添加一個燈箱,並且要完成href鏈接。

燈箱我使用:

http://lokeshdhakar.com/projects/lightbox2/

如何才能做到這一點?

+0

這不是html; html沒有循環。 –

+0

'href'是一個錨點元素屬性..但我從來沒有見過它以你嘗試的方式使用。也許你正試圖設置一個屬性..你可以使用['.attr()'](http://api.jquery.com/attr/)函數來實現。 –

+0

添加'href'與顯示圖像有什麼關係?你真的沒有描述你最終想要解決的問題。 – 2017-04-26 20:57:03

回答

0

使用ATTR:

$("#photos").append("href", "http://www.google.com/") 

了這一點。

$("#photos").attr("href", "http://www.google.com/"); 
+0

不幸的是,似乎並不適合我。 – DarkEnergy

+0

你想要分配一個href到什麼類型的元素? – Korgrue

+0

在我的問題中增加了更多細節。 – DarkEnergy

1

在正在工作的示例中,您將整個圖像放在#photos中。在href是鏈接標籤的屬性,所以我認爲你需要改變這樣的:如果你想一個href爲一個元素添加

$('#photos').append('<a href="http://www.google.com/">Google</a>'); 
+0

這似乎不適用於我。 – DarkEnergy

+0

什麼是#photos元素?你遇到了什麼錯誤? –

相關問題