简体   繁体   English

添加preventDefault()和stopPropagation()

[英]Adding preventDefault() and stopPropagation()

I am new in coding in general, most especially javascript. 我一般来说都是新手,尤其是javascript。

I made a flip div effect. 我做了一个翻转div效果。 But I noticed that when clicking one .button to trigger the flip effect, all the other divs that are similar in a page are also triggered. 但是我注意到,单击一个.button触发翻转效果时,页面中所有其他类似的div也会被触发。 So I am trying to add both .preventDefault() and .stopPropagation() when flipping a div, so far its a big FAIL 所以我试图在翻转div时同时添加.preventDefault().stopPropagation() ,到目前为止,它是一个很大的失败

As a starter, I came up with this code.. 首先,我想到了这段代码。

$('.insidecontent .button').click(function () {
    $('.insidecontent').addClass('flipped');
    $('.insidecontent').removeClass('unflipped');
});
$('.insidecontent .button-c').click(function () {
    $('.insidecontent').removeClass('flipped');
    $('.insidecontent').addClass('unflipped');
});

Demo here: http://jsfiddle.net/uriri/ke7kqvvk/3/ 此处的演示: http : //jsfiddle.net/uriri/ke7kqvvk/3/

A friend of mine suggested that I should use .parents() on the current target inside the .click() function. 我的一个朋友建议我在.parents() .click()函数内的当前目标上使用.parents() But after reading the jQuery documentation, I am totally lost! 但是在阅读了jQuery文档之后,我完全迷路了!

You don't need to stop click event or prevent default. 您无需停止点击事件或阻止默认事件。 You just need to flip right element. 您只需要翻转右元素。 Right not you are adding/removing classes to/from all .insidecontent , while you need to select only parent element of the clicked button: 是的,不是您要在所有.insidecontent中添加类/从所有.insidecontent删除类,而只需要选择clicked按钮的父元素:

$('.insidecontent .button').click(function () {
    $(this).closest('.insidecontent').addClass('flipped').removeClass('unflipped');
});
$('.insidecontent .button-c').click(function () {
    $(this).closest('.insidecontent').removeClass('flipped').addClass('unflipped');
});

Demo: http://jsfiddle.net/ke7kqvvk/5/ 演示: http //jsfiddle.net/ke7kqvvk/5/

Or shorter optimized version can be: 或更短的优化版本可以是:

$('.insidecontent').find('.button, .button-c').click(function () {
    $(this).closest('.insidecontent').toggleClass('flipped unflipped');
});

Demo: http://jsfiddle.net/ke7kqvvk/6/ 演示: http //jsfiddle.net/ke7kqvvk/6/

preventDefault() and stopPropagation() won't help here. preventDefault()stopPropagation()在这里无济于事。 You need to use a selector that's more specific than all the elements with the same class. 您需要使用比具有相同类的所有元素更具体的选择器。

 $('.insidecontent .button').click(function () { $(this).closest('.insidecontent').addClass('flipped').removeClass('unflipped'); }); $('.insidecontent .button-c').click(function () { $(this).closest('.insidecontent').removeClass('flipped').addClass('unflipped'); }); 
 .content{ position: relative; background: #ffffff; width:100%; text-align:center; display: -webkit-box; display: -moz-box; display: box; -webkit-box-orient: vertical; -moz-box-orient: vertical; box-orient: vertical; -webkit-box-align: center; -moz-box-align: center; box-align: center; } ._hasdata .insidecontent { position: relative; background: #ffffff; margin-bottom:30px; -webkit-perspective: 1000px; -moz-perspective: 1000px; -o-perspective: 1000; perspective: 1000px; } ._hasdata .insidecontent .front { position: relative; z-index: 900; width: 200px; height: 200px; background: #ededed; -webkit-transform: rotateX(0deg) rotateY(0deg); -moz-transform: rotateX(0deg) rotateY(0deg); transform: rotateX(0deg) rotateY(0deg); } ._hasdata .insidecontent .front .fcontent { position:relative; top:30%; } ._hasdata .insidecontent.flipped .front { z-index: 900; -webkit-transform: rotateX(0deg) rotateY(180deg); -moz-transform: rotateX(0deg) rotateY(180deg); transform: rotateX(0deg) rotateY(180deg); } ._hasdata .insidecontent .back { position: absolute; width: 200px; height: 200px; top: 0; left: 0; z-index: 800; -webkit-transform: rotateX(0deg) rotateY(-180deg); -moz-transform: rotateY(-180deg); transform: rotateX(0deg) rotateY(-180deg); } ._hasdata .insidecontent.flipped .back { z-index: 1000; background: #E7E7E7; -webkit-transform: rotateX(0deg) rotateY(0deg); -moz-transform: rotateX(0deg) rotateY(0deg); transform: rotateX(0deg) rotateY(0deg); } ._hasdata .insidecontent .front, ._hasdata .insidecontent .back { -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; transform-style: preserve-3d; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; backface-visibility: hidden; -webkit-transition: all .8s ease-in-out; -moz-transition: all .8s ease-in-out; transition: all .8s ease-in-out; } ._hasdata .insidecontent .button, ._hasdata .insidecontent.flipped .button-c { z-index: 15; display: block; position: absolute; bottom: 0; margin: -30px 20px 20px; opacity: 0; -webkit-transition: opacity .1s linear; -moz-transition: opacity .1s linear; transition: opacity .1s linear; } ._hasdata .insidecontent:hover .button, ._hasdata .insidecontent.flipped:hover .button-c { opacity: 0.8; } ._hasdata .insidecontent.unflipped .button-c { display: none; } ._hasdata .insidecontent.flipped ._lar { height: 100%; } ._hasdata .insidecontent.unflipped .bcontent { display: none; } ._hasdata .insidecontent.flipped .bcontent { top: 30%; position: relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content"> <div class="_hasdata"> <div class="insidecontent initalized unflipped"> <div class="front"> <span class="fcontent">FRONT CONTENT<br />HERE</span> <a class="btt button">info</a> </div> <div class="back _lar"> <div class="bcontent">BACK INFO HERE</div> <a class="btt button-c">go back</a> </div> </div> </div> <div class="_hasdata"> <div class="insidecontent initalized unflipped"> <div class="front"> <span class="fcontent">FRONT CONTENT<br />HERE</span> <a class="btt button">info</a> </div> <div class="back _exif"> <div class="bcontent">BACK INFO HERE</div> <a class="btt button-c">go back</a> </div> </div> </div> </div> 

Have you learned about the " this " keyword yet? 您是否了解“ this ”关键字? In a JQuery click handler, $(this) refers to the element that the handler is called on. 在JQuery点击处理程序中, $(this)指向调用处理程序的元素。 So, if you put removeClass and addClass methods on $(this) it will only affect that one element. 因此,如果将removeClass和addClass方法放在$(this)上,它将仅影响该元素。 You can traverse using this too... so your code would look like this: 您也可以使用它遍历...因此您的代码将如下所示:

    $('.insidecontent .button').click(function () {
    $(this).parent().find('.insidecontent').addClass('flipped');
    $(this).parent().find('.insidecontent').removeClass('unflipped');
});

$('.insidecontent .button-c').click(function () {
    $(this).parent().find('.insidecontent').removeClass('flipped');
    $(this).parent().find('.insidecontent').addClass('unflipped');
});

you may not even need the find method in your case, but it is better to be explicit. 您可能甚至不需要您的情况下的find方法,但最好是明确的。

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

相关问题 在JavaScript中是stopPropagation还是preventDefault? - stopPropagation or preventDefault in JavaScript? preventdefault和stoppropagation不适用于pointermove - preventdefault and stoppropagation not working with pointermove 为什么在preventDefault和stopPropagation之前使用&#39;if&#39; - Why 'if' before preventDefault and stopPropagation 在 javascript 中删除 preventDefault 和 stopPropagation - Removing preventDefault and stopPropagation in javascript stopPropagation如何取消preventDefault? - How does stopPropagation cancel preventDefault? Typescript 3 Angular 7 StopPropagation 和 PreventDefault 不起作用 - Typescript 3 Angular 7 StopPropagation and PreventDefault not working 为什么 preventdefault/stopPropagation 在 javascript 中不起作用? - why preventdefault/stopPropagation not working in javascript? jQUery preventDefault或stopPropagation()无法与切换表单一起使用 - jQUery preventDefault or stopPropagation() not working with toggle form 如何覆盖event.stopPropagation(),preventDefault()。stopImmediatePropagation() - How to override event.stopPropagation(),preventDefault().stopImmediatePropagation() stopPropagation和preventDefault不起作用,父级点击仍在触发 - stopPropagation & preventDefault are not working, parent click is still firing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM