简体   繁体   English

返回false; 和preventDefault(); 不工作。 Onclick重定向到图像链接URL

[英]return false; and preventDefault(); not working. Onclick redirects to image link url

I'm trying to do an action on the product images (below main image) that when the user clicks on the Right Arrow, the 5th image will be clicked automatically and it will be shown as the main image. 我正在尝试对产品图像(在主图像下方)进行操作,当用户单击向右箭头时,将自动单击第5张图像并将其显示为主图像。 Here's the link for reference: http://sazua.apleu.servertrust.com/3B-Scientific-Classic-Brain-5-part-p/c18.htm 这是参考链接: http : //sazua.apleu.servertrust.com/3B-Scientific-Classic-Brain-5-part-p/c18.htm

I've tried return false; 我尝试过返回false; and preventDefault(); 和preventDefault(); but the click is still redirecting the user to the next image url link. 但是点击仍然会将用户重定向到下一个图像网址链接。

HTML: HTML:

<li class="current">
    <a style="display: none;" href="/v/vspfiles/photos/C18-6.jpg" rel="shadowbox[ProductImages]" title="Classic Human Brain, 5 part" data-sbkey="5">
        <img class="vCSS_img_alternate_product_photo" id="alternate_product_photo_6" style="border-color:#666666;" src="/v/vspfiles/photos/C18-6S.jpg" border="1" onclick="javascript:change_product_photo(6);return false;">
    </a>
    <a href="/v/vspfiles/photos/C18-6.jpg" data-sbkey="5" title="Classic Human Brain, 5 part">
        <img class="vCSS_img_alternate_product_photo" id="alternate_product_photo_6" style="border-color:#666666;" src="/v/vspfiles/photos/C18-6S.jpg" border="1" onclick="javascript:change_product_photo(6);return false;">
    </a>
</li>

Javascript I'm using: 我正在使用的Javascript:

$(document).ready(function() {
    $("#right-btn-carousel").click(function (e) {
       $("#altviews > ul > li:nth-child(5) > a:nth-child(2) > img").click();
       e.preventDefault();
    });
});

Note: I can't modify the HTML since I'm using Volusion (hardcoded) so I'm trying to work with what I have. 注意:由于我使用的是Volusion(硬编码),因此无法修改HTML,因此我尝试使用现有的内容。

Thanks. 谢谢。

You need to use e.preventDefault() e.stopImmediatePropagation() before any action/side effects inside click method. 您需要在点击方法内的任何操作/副作用之前使用e.preventDefault()e.stopImmediatePropagation()。 I meant first step. 我的意思是第一步。

Try to empty your <a href="#" /> if href is not needed 如果不需要href,请尝试清空您的<a href="#" />

$(document).ready(function() {
    $("#right-btn-carousel").click(function (e) {
      e.preventDefault();
     $("#altviews > ul > li:nth-child(5) > a:nth-child(1) > a").attr("href", "#"); 
      $("#altviews > ul > li:nth-child(5) > a:nth-child(2) > img").click();
     ;
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM