Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery排除具有特定类的元素_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用jquery排除具有特定类的元素

Javascript 使用jquery排除具有特定类的元素,javascript,jquery,html,Javascript,Jquery,Html,当在产品页面上单击图像或链接时,我试图在警告框中获取产品名称。但也有一个buy now按钮可用,如果单击该按钮,当前也会发出弹出警报。我想使用jquery排除它。这就是我要说的 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> jQuery(document).ready

当在产品页面上单击图像或链接时,我试图在警告框中获取产品名称。但也有一个buy now按钮可用,如果单击该按钮,当前也会发出弹出警报。我想使用jquery排除它。这就是我要说的

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function ($) {
    $(".item").click(function (event) {
        var href = $('a', this).attr("href");
        var target = $('a', this).attr("target");
        var text = $(this).find('.product-name').text();
        event.preventDefault();
        alert(text);
        setTimeout(function () {
            window.open(href, (!target ? "_self" : target));
        }, 300);
    });
});
</script>

<li class="item">

<a href="product1.php" title="Sample Product Name" class="product-image">
    <span class="sale-item">Sale!</span>
    <div class="cat-mouseover"></div>
    <img src="/images/product1.png" alt="Sample Product Name">
</a>
<h2 class="product-name"><a href="product1.php" title="Sample Product Name">Sample Product Name</a></h2>
<div class="price-box">

    <p class="old-price">
        <span class="price-label">For:</span>
        <span class="price" id="old-price-426">&nbsp;199,-</span>
    </p>

    <p class="special-price">
        <span class="price-label"></span>
        <span class="price" id="product-price-426"> Now&nbsp;139,- </span>
    </p>
</div>

<div class="actions">
    <button type="button" title="Buy Now" class="button btn-cart" onclick="setLocation('/checkout/cart/add/uenc/aHR0cDovL3d3dy52aXRhLm5vLw,,/product/426/')"><span><span>Buy</span></span>
    </button>
</div>
</li>

<li class="item">

<a href="product1.php" title="Sample Product Name" class="product-image">
    <span class="sale-item">Sale!</span>
    <div class="cat-mouseover"></div>
    <img src="/images/product1.png" alt="Sample Product Name">
</a>
<h2 class="product-name"><a href="product1.php" title="Sample Product Name">Sample Product Name</a></h2>
<div class="price-box">

    <p class="old-price">
        <span class="price-label">For:</span>
        <span class="price" id="old-price-426"> &nbsp;199,- </span>
    </p>

    <p class="special-price">
        <span class="price-label"></span>
        <span class="price" id="product-price-426"> Now&nbsp;139,- </span>
    </p>
</div>

<div class="actions">
    <button type="button" title="Buy Now" class="button btn-cart" onclick="setLocation('/checkout/cart/add/uenc/aHR0cDovL3d3dy52aXRhLm5vLw,,/product/426/')"><span><span>Buy</span></span>
    </button>
</div>
</li>

jQuery(文档).ready(函数($){
$(“.item”)。单击(函数(事件){
var href=$('a',this.attr(“href”);
var target=$('a',this.attr(“target”);
var text=$(this.find('.product name').text();
event.preventDefault();
警报(文本);
setTimeout(函数(){
打开(href,(!target?”\u self:target));
}, 300);
});
});
  • 用于: 199,-

    现在139

    购买
  • 用于: 199,-

    现在139

    购买

  • 请告知

    将此项放在回调的顶部:

    if ($(event.target).hasClass("btn-cart") || $(event.target).parents(".btn-cart").length !== 0) {
        return;
    }
    
    现在,如果“event.target”(=单击发生的位置)具有类“btn cart”或是该“btn cart”的子级,则回调的执行将返回


    请参阅更新的fiddle:

    如果单击按钮,请退出事件处理程序:

    if(($(event.target).is('.button.btn-cart')) return;
    // rest of your code goes here
    

    jQuery有一个
    not
    method@JeremyJStarcher我试图使用类似这样的not函数,但无法使用$(“.item”)。not(“.actions”)。单击(函数(事件){您的代码工作正常我无法删除这两个跨距:(,还有其他解决方案吗?非常感谢您的帮助除了HTML代码之外,我完全可以访问jqueryHad并完成了相同的代码,但我不知道您已经回答了,标记为我的deletion@Juvian没有兄弟,我已经标记了任何东西。你能帮我吗。更新了我的答案,希望这有帮助。@Johan没有-他有跨度,所以e通风口可能来自按钮或按钮spans@SebastianG.Marinescu好吧,我读得不够仔细。可能最简单的方法是使用一个公共类来触发事件,而不是创建异常。@Johan,或者停止在按钮回调中冒泡事件(按钮还具有绑定功能)