您可以使用jQuery和wrap()
函數遍歷列表中的所有圖像並在其周圍放置一個鏈接,以下示例將導航到圖像本身。
$(document).ready(function(){
$('.selected img').each(function(){
$(this).wrap('<a href="' + $(this).attr('src') +'"></a>');
})
})
如果你想添加一個鏈接到另一個網頁/網站與$(this).wrap('<a href="www.google.com"></a>')
取代$(this).wrap('<a href="' + $(this).attr('src') +'"></a>')
這樣
$(document).ready(function(){
$('.selected img').each(function(){
$(this).wrap('<a href="www.google.com"></a>');
})
})
如果你想點擊圖片你之後使用JavaScript來直接瀏覽到另一個網站你可以像這樣
click事件設置
window.location.href
$(document).ready(function(){
$('.selected img').click(function(){
window.location.href='www.google.com';
})
})
你指源或重定向鏈接 – Omi
請提供的代碼示例你嘗試過的。 – Fabien
檢查這個https://stackoverflow.com/a/179717 – user3402754