Jquery链接到外部网页

时间:2013-12-11 19:13:50

标签: javascript jquery html

我有以下代码。

<script type="text/JavaScript">
// prepare the form when the DOM is ready 
$(document).ready(function() {
var galleryClass = '.gallery';
$(galleryClass+' li img').click(function(){
    var $gallery = $(this).parents(galleryClass);
    $('.main-img',$gallery).attr('src',$(this).attr('src').replace('thumb/', ''))
    .parent().attr('href',$(this).parent().attr('href'));
});
var imgSwap = [];
 $(galleryClass+' li img').each(function(){
    imgUrl = this.src.replace('thumb/', '');
    imgSwap.push(imgUrl);
});
$(imgSwap).preload();
});
$.fn.preload = function() {
this.each(function(){
    $('<img/>')[0].src = this;
});

}
</script>

以下HTML代码。

<div class="gallery">
<img src="gallery/img_1.png" alt="" class="main-img" />
<img src="images/Select-Brands.jpg" width="1010" height="50" />
<ul>
  <li><img src="gallery/thumb/img_1.png" alt="" /></li>
  <li><img src="gallery/thumb/img_2.png" alt="" /></li>
  <li><img src="gallery/thumb/img_3.png" alt="" /></li>
  <li><img src="gallery/thumb/img_4.png" alt="" /></li>
  <li><img src="gallery/thumb/img_5.png" alt="" /></li>
  <li><img src="gallery/thumb/img_6.png" alt="" /></li>
  <li><img src="gallery/thumb/img_7.png" alt="" /></li>
  <li><img src="gallery/thumb/img_8.png" alt="" /></li>
  <li><img src="gallery/thumb/img_9.png" alt="" /></li>

</ul>
</div>

这是网站示例。 http://flairnecessities.com/AskarBrands/askarbrands.html 当您点击每个徽标时,它会显示一个新图像。我想让每个大图像(其中8个)转到8个独立的外部网站。他们还需要打开一个单独的窗口。

执行此操作所需的代码是什么?谢谢!

2 个答案:

答案 0 :(得分:1)

HTML

<img src="img.png" data-lg="large.png" data-href="http://google.com">
<a class="lg-link"><img class"img-lg"></a>

的jQuery

$("[data-lg]").click(function(){
  $(".img-lg").attr("src", $(this).data("lg"));
  $(".lg-link").attr("href", $(this).data("href"));
});

CSS

[data-lg]{
  cursor:pointer;
}

答案 1 :(得分:0)

抱歉,快速阅读问题。

<ul>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_1.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_2.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_3.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_4.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_5.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_6.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_7.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_8.png" alt="" /></li>
  <li><img rel="http://www.google.com" src="http://flairnecessities.com/AskarBrands/gallery/thumb/img_9.png" alt="" /></li>

</ul>

添加一个定义每个缩略图网址的rel标签(上图):

在主图像链接周围添加此包装:

<a id ="linker" href="#" target="_blank"><img src="http://flairnecessities.com/AskarBrands/gallery/img_1.png" alt="" class="main-img" /></a>

最后将此添加到您的初始点击功能:

var link = $(this).attr('rel');
$('#linker').attr('href',link);