javascript - jQuery缩略图替换方法(需要添加A标签的解决方案)

标签 javascript jquery

我有一个缩略图替换解决方案,我需要在 img 项目周围添加一个 a 标签,并且仍然具有相同的功能。

jQuery

$(".large").click(function () {
    var thumbRel = $(this).attr("rel");
    $(this).effect("transfer", { to: $(".thumbnail img[rel='" + thumbRel + "']") }, 1000);
});

$(".thumb").click(function () {
    $(".thumbnail [class^='thumb']").removeClass("current");
    $(this).addClass("current");
    $(".large").attr("rel", $(this).attr("rel"));

    $(this).effect("transfer", { to: $(".large") }, 1000);
    $(".large").animate({ opacity: 1 }, 1000);
    $(this).queue(function () {
        $(".large").attr("src", $(this).attr("src").replace("-thumb", ""));

        $(".large").animate({ opacity: 1 }, 1000);
        $(this).dequeue();
    });
});

标记与 Dot Net 混合,但在这里

<div class="product_images">        
    <div class="mainImage">            
        <a class="ShowProductImageDlg" href="#" onclick="ProductImagesDlgClientSide.showDialog('@Model.ProductToDisplay.Product.UrlSlug', 'Bottle'); return false;" >
            <img class="LargeImage" src="@Model.ProductToDisplay.Product.awsImageUrlLarge" alt="@Model.ProductToDisplay.Product.ProductName" />
        </a>            
    </div>
    <strong>Additional Images</strong>
    <div class="thumbnail">
        <ul>
            <li><a href=""><img class="SmallImage Bottle" src="@Model.ProductToDisplay.Product.awsImageUrlSmall" 
                    alt="@Model.ProductToDisplay.Product.ProductName" /></a></li>                

            <li><a href=""><img class="SmallImage BackLabel" src="@Model.ProductToDisplay.ProductAdditionalImageUrlSmall(awsProductImageType.BackLabel)"
                    alt="@Model.ProductToDisplay.Product.ProductName" /></a></li>            
        </ul>
    </div>    
</div>

最终目标是当图像本身只有 80x35 时,使图像的整个 div 都可点击(即 90 像素 x 90 像素)。

我想使用相同的 jQuery。

最佳答案

你可以这样做(see jsfiddle as a proof):

var link = jQuery('<a>', {
    'href': 'http://www.google.com/',
    'title': 'Dynamically generated link'
});

jQuery('img').wrap(link);

基本上,您首先创建一个链接(<a>元素),然后将其包裹在img选择的图像周围选择器(您可以根据需要进行调整)。

关于javascript - jQuery缩略图替换方法(需要添加A标签的解决方案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960865/

相关文章:

javascript - AngularJS 观察者 : oldValue equal to newValue in unit test

javascript - TinyMCE 工具栏选择框

javascript - 如何在 ActiveAdmin 中使用 Javascript 检索索引页面上选定 ID 的列表?

javascript - 如何使用 Javascript 使文本框只读

javascript - 尝试使用 localStorage 限制表单的使用 x 次

javascript - Angular 1.5.8 组件与 typescript (ecma6) 和 (ecma5) 配置不同

javascript - 奇怪的javascript语法错误

javascript - 有前途的简单功能

jquery - 如何使用 jQuery 自动切换样式表?

Javascript:在 JS 文件中加载外部 JS 文件