0

即使我找到了我的问题的问题,我也无法找到解决方案。

好吧,我有一个“添加到购物车”按钮,它可以采用两种形式的代码,如下所示:

CODE 1:
<div class="cart">
    <a class="button" onclick="addToCart('710'); _gaq.push(['_trackEvent', 'Category Page', 'Add to Cart', 'Large Dual Fitting Pluto Metal Lighting Pendant Shades - Green']);">
        <span>Add to Basket</span>
    </a>
</div>

CODE 2:
<div class="cart">
    <a class="button" onclick="addToCart('710');">
        <span>Add to Basket</span>
    </a>
</div>

我有一个单独的 common.js 文件,其中定义了函数 addToCart()。我遇到的特定代码是这样的:

if (json['success']) {  
    html = '<div class="popup-cart-info"><div class="popup-close"><a alt="Close &amp; Continue" onclick="closeCart();" title="Close &amp; Continue" ><img src="catalog/view/javascript/jquery/fancybox/fancy_close.png"></a></div>';
    MainImg = $('a[onclick="addToCart(\'' + product_id + '\');"]').parents().find('.image a img');
    AltImg = $('a[onclick="addToCart(\'' + product_id + '\');"]').parent().parent().parent().find('.left .image a img');
    if(MainImg.length) {
        html += '<div class="cart-box-img"><img style="width:150px;" src="' + MainImg.attr('src') + '" /></div>';
    } else if(AltImg.length) {
        html += '<div class="cart-box-img"><img style="width:150px;" src="' + AltImg.attr('src') + '" /></div>';
    } else {
        html += '<div class="cart-box-img"><img style="width:130px;" src="image/no_image1.jpg" title="Image Unavailable" alt="Image Unavailable" /></div>';
    }
    html += '<div class="cart-box-succ-det">' + json['success'] + '</div>';
}

分配 MainImg 的位似乎适用于 CODE2,但不适用于 CODE1。两者之间的唯一区别是 _gaq.push() 函数。如果我在 Firefox 中使用 FireBug 并删除 _gaq.push() 函数,它似乎运行良好。我要做的就是传递已添加到购物车的产品的图像源。但是当它旁边还有一个功能时,它就不起作用了。

我的问题是,即使在 onclick 事件中定义了两个函数,我如何使用 parents() 函数选择标签。

您可以在http://bit.ly/YEW2f2看到我想要实现的目标。该图像是为“新品”而不是“特色产品”获得的。

任何帮助深表感谢。感谢您的关注。问候, 卡西克

4

2 回答 2

0

you can try this: http://api.jquery.com/attribute-starts-with-selector/ instead of [onclick=...]

于 2013-03-17T02:08:59.130 回答
0

The selector

a[onclick="addToCart('170');]

does of course not match

<a class="button" onclick="addToCart('710'); _gaq.push(['_trackEvent', 'Cat ...

because the onclick attribute does not contain the exact text a[onclick="addToCart('170');], but more than that.

To select an attribute when the content begins with a specified text, use the comparison "operator" ^= instead of =.

于 2013-03-17T02:10:59.833 回答