1

在画廊中,我想阻止某些缩略图通过灯箱扩展其原始图片。图像没有 ID,只有一个类。所有链接都是从数据库中的表中读取的。

$(document).ready(function(){
            if($('.product-image').attr('target', 'blockedpath')) {
                $('.product-image').click(function(e) {
                    e.preventDefault();
                    return false;
                });
            }
        }); 


<li class="product">
            <div class="productInfo">
                <h3>@i.ToString()</h3>
                <a href="@Href("~/Images/2013/" + i.ToString() + ".jpg")" rel="lightbox" class="link">
                    <img class="product-image" src="@Href("~/Images/2013/Thumbnails/" + i.ToString() + ".jpg")" alt="foto" />
                </a>
            </div>
        </li>

如果我使用它,所有缩略图都会被阻止。如何防止仅被阻止的图片并避免阻止缩略图。是否有可能将所有应该被阻止的图像保存在一个数组中并循环遍历该数组以阻止这些缩略图?

4

2 回答 2

5

您首先检查是否存在任何图像target = blockedpath,然后阻止所有图像。

你可以使用这样的东西:

$(document).ready(function(){
    // Select elements with the product-images class, that also 
    // have a target attribute with a value equal to 'blockedpath'
    // Bind a click event to the matched elements
    $('.product-image[target="blockedpath"]').click(function(event) {
        event.preventDefault();
        return false;
    });
}); 
于 2013-03-07T09:57:05.997 回答
1
$('[target=blockedpath]').click(function( e ){
  e.preventDeault();
});

或相反:

$('.product-image').not('[target=blockedpath]').click(function(){
  alert('hi!');
});

http://api.jquery.com/event.preventDefault/

于 2013-03-07T09:58:16.663 回答