我有一些代码。基本上整个DIV都是可点击的,但我需要在DIV中的一个按钮来执行与DIV其余部分不同的功能,我已经尝试了.stopPropagation()
但它太彻底了,因为它禁用了我正在尝试的OTHER功能在.CLICK()
上运行。
有问题的按钮(位于“+添加项目”旁边)并且不幸折叠了,但仍然可以点击。您会看到它应该触发灯箱,但它也会将产品添加到它上面的DIV。我想解雇灯箱,但不要将产品添加到我的“篮子”中,任何人都可以引导我朝着正确的方向前进吗?我的代码如下;
HTML:
<form method="post" id="partnumbers" name="partnumbers" action="formHandler">
<div id="specialOffer">
<ul>
<li>
<div id="prodD">
<input type="hidden" name="partNum_1" id="catnum1" value="" />
<div id="close" style="display:none;"><a href="#">Remove X</a></div>
<div id="Prod1"></div>
</div>
</li>
<li>
<div id="prodO">
<input type="hidden" name="partNum_2" id="catnum2" value="" />
<div id="close" style="display:none;"><a href="#">Remove X</a></div>
<div id="Prod2"></div>
</div>
</li>
<li>
<div id="prodA">
<input type="hidden" name="partNum_3" id="catnum3" value="" />
<div id="close" style="display:none;"><a href="#">Remove X</a></div>
<div id="Prod3"></div>
</div>
</li>
<li>
<div id="prodSubmit">
<input name="Submit" id="Submit" type="submit" value="Submit" />
</div>
</li>
</ul>
</div>
<div id="flyout1">
<ul class="paginate-1">
<li><div id="subProd" class="9059863">
<img class="thumb" src="images/placeholder.jpg" width="110" height="110" />
<span class="product">
<p class="title">PRODUCT TITLE</p>
<p class="number">PARTnum</p>
<p class="price">Price</p>
</span>
<a href="#" class="button">+ Add item</a>
<a href="http://www.google.co.uk" class="qv"><img src="images/qv.png" width="17" height="17" /></a>
</div></li>
<li><div id="subProd" class="9087361">
<img class="thumb" src="images/placeholder.jpg" width="110" height="110" />
<span class="product">
<p class="title">PRODUCT TITLE</p>
<p class="number">PARTnum</p>
<p class="price">Price</p>
</span>
<a href="#" class="button">+ Add item</a>
<a href="http://www.google.co.uk" class="qv"><img src="images/qv.png" width="17" height="17" /></a>
</div></li>
<li><div id="subProd" class="9087378">
<img class="thumb" src="images/placeholder.jpg" width="110" height="110" />
<span class="product">
<p class="title">PRODUCT TITLE</p>
<p class="number">PARTnum</p>
<p class="price">Price</p>
</span>
<a href="#" class="button">+ Add item</a>
<a href="http://www.google.co.uk" class="qv"><img src="images/qv.png" width="17" height="17" /></a>
</div></li>
<li><div id="subProd" class="9087354">
<img class="thumb" src="images/placeholder.jpg" width="110" height="110" />
<span class="product">
<p class="title">PRODUCT TITLE</p>
<p class="number">PARTnum</p>
<p class="price">Price</p>
</span>
<a href="#" class="button">+ Add item</a>
<a href="http://www.google.co.uk" class="qv"><img src="images/qv.png" width="17" height="17" /></a>
</div></li>
<li><div id="subProd" class="9059791">
<img class="thumb" src="images/placeholder.jpg" width="110" height="110" />
<span class="product">
<p class="title">PRODUCT TITLE</p>
<p class="number">PARTnum</p>
<p class="price">Price</p>
</span>
<a href="#" class="button">+ Add item</a>
<a href="http://www.google.co.uk" class="qv"><img src="images/qv.png" width="17" height="17" /></a>
</div></li>
</ul>
</div>
</form>
CSS:
#specialOffer, #flyout1, #flyout2, #flyout3 {margin:0; padding:0; width:990px; overflow:hidden;}
#specialOffer {padding:10px 0 0 20px;}
#flyout1 {background:transparent url(../images/popupBG0.png) top left no-repeat;}
#specialOffer UL, UL.paginate-1, UL.paginate-2, UL.paginate-3 {
float:left;
margin:0; padding:0;
height:260px;
display:block;
}
#specialOffer UL {width:970px;}
#specialOffer UL, UL.paginate-3 {
height:270px;
}
#specialOffer UL LI, UL.paginate-1 LI, UL.paginate-2 LI, UL.paginate-3 LI {
float:left;
margin:30px 30px 0 27px; padding:0;
display:inline;
width:130px;
height:200px;
}
#specialOffer UL LI {
height:220px;
}
#scrollable {
margin-top:10px;
float:left;
background-color:#eee;
width:990px;
border-top: 1px;
border-top-color: #CCC;
border-top-style: solid;
}
.pager {
margin-right:10px;
overflow:hidden;
padding:1em 0;
float:right;
}
.pager li{
float:left;
list-style-type:none;
margin-right:.3em;
font-size:1.1em;
}
.pager a{
color:#FF9182;
outline:none;
text-decoration:underline;
}
.pager a:hover{
text-decoration:none;
}
.pager .active a {
color:#666;
font-weight:bold;
}
.pager .disabled {
width:32px;
text-indent:-9999px;
}
/* Needed for carousel*/
.flexiwrap {
margin:0 0 0 20px; padding:0;
float:left;
width:970px;
height:271px;
}
/* PAGINATED CONTENT */
#Prod1, #Prod2, #Prod3, #subProd {float:left; margin:0; padding:0; width:130px;}
#Prod4 {float:left; margin:0; padding:0; width:130px;}
#Prod1 img.thumb, #Prod2 img.thumb,
#Prod3 img.thumb, #subProd img.thumb {
margin:0; padding:0 10px;
background-color:#fff;
border:1px solid #ccc;
}
.button, .qv {
float:left;
background-image: linear-gradient(#ffffff, #eeeeee);
border: 1px solid #ccc;
border-radius: 3px;
color: #666;
cursor: pointer;
display: inline-block;
font-size: 13px;
line-height: 17px;
padding: 5px 10px;
text-decoration:none;
overflow: visible;
}
.qv {
z-index:9999px;
}
#Prod1 .qv, #Prod2 .qv,
#Prod3 .qv, #subProd .qv {
float:right;
margin:0;
padding:0;
cursor: pointer;
padding:5px 5px 0px 6px;
}
#Prod1 .qv IMG, #Prod2 .qv IMG,
#Prod3 .qv IMG, #subProd .qv IMG {
border:0 none;
margin:0; padding:0;
}
#Prod1 .product P, #Prod2 .product P,
#Prod3 .product P, #subProd .product P {margin:5px 0; padding:0; line-height:13px;}
#Prod1 .product .title, #Prod2 .product .title,
#Prod3 .product .title, #subProd .product .title {color:#666; font-size:13px;}
#Prod1 .product .number, #Prod2 .product .number,
#Prod3 .product .number, #subProd .product .number {color:#666; font-size:11px;}
#Prod1 .product .price, #Prod2 .product .price,
#Prod3 .product .price, #subProd .product .price {
font-size:20px;
font-weight:bold;
margin-bottom:3px;
line-height:20px;
}
#close {margin-top:-15px;}
#close a {float:right; margin:0; padding:0; text-decoration:none;}
#Prod1, #Prod2, #Prod3 {border:1px solid #ccc; height:110px; cursor:pointer;}
#prodDoll INPUT, #prodOutfit INPUT, #prodAcc INPUT {display:none;}
#Prod4 {float:left; margin:0; padding:0; width:130px; height:110px; cursor:pointer;}
#subProd {cursor: pointer;}
JS:
$('#flyout1').hide();
$('#prodSubmit').hide();
$('#flyout1 #subProd').click(function() {
var id1 = $(this).attr('class');
$("#catnum1").val(id1);
$("#Prod1").html($("." + $(this).attr('class')).html());
$("#Prod1").css("border", "none");
$("#prodD #close").css("display", "block");
$("#prodD .button, #prodD .qv").css("display", "none");
$("#Prod1").attr('rel', 'done');
});
$("#prodD #close a").click(function() {
$("#prodD #close").css("display", "none");
$("#Prod1").empty();
$("#Prod1").css("border", "1px solid #CCC");
$("#Prod1").removeAttr("rel")
});
$("#Prod1").on("click",function(){
$("#flyout1").slideToggle();
});
$("a.qv").click(function(e) { e.stopPropagation(); });
$("a.qv").colorbox();
and I have set up a fiddle here :)
任何帮助都会非常感激,因为我可能错过了显而易见的事项:/
答案 0 :(得分:5)
同意,你是有效的,因为colorbox似乎依赖于事件冒泡,所以e.preventDefault()
会阻止它的动作。直截了当地,您可以使用颜色框或阻止点击事件冒泡,但不能同时阻止两者。
stackoverflow搜索显示其他人遇到了同样的问题,但我找不到与你的精确匹配的场景。
我尝试了各种各样的东西并提出了以下方法(又名“hack”):
e.preventDefault()
,e.stopPropagation()
然后将其href
传输到虚拟链接,然后再触发它。代码非常简单:
var $dummyLink = $("<a/>").colorbox().appendTo("body").hide();
$("a.qv").on('click', function(e) {
e.preventDefault();
e.stopPropagation();
$dummyLink.attr('href', this.href).trigger('click');
});
<强> DEMO 强>
这似乎克服了当前的问题。
根据您要实现的目标,可能会进一步将colorbox与所单击的特定“qv”相关联。如果这是一个问题,那么它可能更好地作为一个单独的问题处理。
答案 1 :(得分:1)
接近这一点的另一种方法是:
$('.parent').once().on('click', function(e){
// Don't apply if child link was clicked on.
if (!$(e.target).is('.child')) {
// Background click action
}
});
$('.child').on('click', function(){
// Child click action i.e. colorbox action
});
答案 2 :(得分:0)
Beetroot-Beetroot真的帮助了我,但我不得不放弃使用colorbox,所以为了与大家分享,我已将代码改为此;
var $dummyLink = $("a.qv").attr("href");
$("a.qv").on('click', function(e) {
e.stopPropagation();
$dummyLink.attr('href', this.href).trigger('click');
});
它对我来说非常有效,我希望它可以帮助处于类似情况的人。