JavaScript中的stopPropagation或preventDefault?

时间:2015-11-10 09:42:25

标签: javascript jquery

我尝试删除鼠标悬停方法,直到我使用mouseout但我使用的是javascript。是否有类似于jQuery的方法?

这是我的活动

function addEventListeners() {
    document.getElementById("container").addEventListener("mouseover", isClicked);
    document.getElementById("container").addEventListener("mouseleave", collapse);
    document.getElementById("close-button").addEventListener("click", collapse);        
    document.getElementById("box").addEventListener("click", clickthrough);
    document.getElementById("replay").addEventListener("click", replay);
    if(isIphone) document.getElementById("video").addEventListener("click", function(){collapse()});
}

<div id="container">
    <div id="ad" class="collapsed">
        <div id="banner">
            <img class="back" id="back" src="frames/frame_1.jpg"/>
            <img class="top" id="top" src="frames/frame_2.jpg"/>
            <img id="logo1" type="image/svg+xml" src="" />                                              
            <div class="border" id="border"></div>
            <div id="cta"></div>
        </div>
        <div id="expansion">
            <div class="exp_bn" id="exp_bn" >
                <div class="con-pos" id="con-pos" >                                         
                    <div id="videoContainer" class="videoContainer">           
                        <video class="video" id="video" autoplay></video>
                    </div>                                                                        
                </div>                    
            </div>
            <div class="endframe"></div>
            <img id="logo2" type="image/svg+xml" src="" />
            <div id="box"></div>
             <div class="topgrp">
                <div id="close-button" class="icon-cross"></div>
            </div>
            <div class="bottomgrp">
                <div id="replay" class="icon-replay"></div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

preventDefaultstopPropagationevent对象的原生JS方法 它不是jQuery的一部分。

你可以在香草JS中使用它:

document.getElementById("link").onclick = function (e) {
  console.log(e.preventDefault); // function preventDefault() { [native code] }
  console.log(e.stopPropagation); // function stopPropagation() { [native code] }
  
  e.stopPropagation();
  e.preventDefault();
};
<a href="google.com" id="link">Link</a>