Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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防止默认操作_Javascript_Html_Jquery_Dom Events - Fatal编程技术网

Javascript防止默认操作

Javascript防止默认操作,javascript,html,jquery,dom-events,Javascript,Html,Jquery,Dom Events,我只是在练习一些JavaScript,我遇到了一个无法回避的错误。我的html中有一个列表,其中每个项目都链接到一个页面,但是 我的js脚本中有一个函数,当我单击该项目时,它应该从列表中删除该项目,我尝试使用preventDefault,但它仍然会将我带到该链接 <ul id="shoppingList"> <li class="complete"><a href="google.com><

我只是在练习一些JavaScript,我遇到了一个无法回避的错误。我的html中有一个列表,其中每个项目都链接到一个页面,但是 我的js脚本中有一个函数,当我单击该项目时,它应该从列表中删除该项目,我尝试使用preventDefault,但它仍然会将我带到该链接

<ul id="shoppingList">
        <li class="complete"><a href="google.com><em>fresh</em>tuna</a></li>
        <li class="complete"><a href="google.com">meatball</a></li>
        <li class="complete"><a href="google.com">kiwi</a></li>
        <li class="complete"><a href="google.com">chicken soup</a></li>
    </ul> 

function getTarget(e){
if(!e){
    e = window.event;
} 
return e.target || e.srcElement;}

function itemDone(e){
//Remove item from the list
var target;
var Parent;
var Grandparent;
target = getTarget(e);
Parent = target.parentNode;
Grandparent = target.parentNode.parentNode;
Grandparent.removeChild(Parent);

//Prevent the link from taking you somewhere
if(e.preventDefault){
    e.preventDefault();
}else{
    e.returnValue = false;
}
}

//Set up event listeners to call ItemDone() on click
var Shoppo = document.getElementById('shoppingList');
if(Shoppo.addEventListener){
Shoppo.addEventListener('click',function(e){
    itemDone(e);
},flase);
}else{
Shoppo.attachEvent('onClick',function(e){
    itemDone(e);
});
}
要检查是否调用了e.preventDefault,可以使用isDefaultPrevented返回true或false。所以你可以像这样修改你的代码

e.preventDefault();
if(e.isDefaultPrevented()){
    // default event is prevented
}else{
    e.returnValue = false;
}

如果您不希望浏览器跟随链接,但仍然需要url,可以将其存储在其他属性中,并将href设置为

<a href="#" id="link1" destinationurl="www.google.com" ... />
更改此项:

if(e.preventDefault){
    e.preventDefault();
}else{
    e.returnValue = false;
}
为此:

e.preventDefault();
return false;

这将起作用,并为您节省几行代码。

我发现自己处于这种情况,即

ev.preventDefault();
ev.stopPropagation();
甚至

document.onclick= function(ev){ev.preventDefault(); ev.stopPropagation(); return false;}
根本没有帮助

当我使用touchstart、touchend、touchmove创建mousedown、onmouseup和mousemove事件时

在桌面上,没有问题,但在手机上使用相同的代码,有时在同一次通话中,所有内容都会翻倍???在控制台中

我可以用以下代码解决问题:

var clickUpMouseEventTimer = 0; 

function clickUp(ev) {
   ev = ev || event;

   console.log('clickUp mouse timer' ,ev['timeStamp'], ev['timeStamp'] -clickUpMouseEventTimer)

   if( clickUpMouseEventTimer + 200 < ev['timeStamp']) {
       clickUpMouseEventTimer = ev['timeStamp'];
   } else {
       return;
   }
通过这种方式,我可以截获任何mousedown或mouseup事件,如果通过测量触发两倍:

ev[“时间戳”]

并通过提供200ms的延迟来决定这取决于您的程序执行时间,是放弃输入还是让它通过函数

除了触摸鼠标事件转换器外,桌面和移动设备现在都在执行完全相同的步骤


希望它能有所帮助,就像你们在经验和知识方面帮助我很多很多次一样。

ife.preventDefault您想用它实现什么?如果preventDefault工作组从未想过,有没有办法知道event.preventDefault是否在使用中?IE explorer的旧版本不使用prevent default,因此,我使用了returnValue StationCheck你的拼写-flase。这仍然将我发送到a链接。我的道歉,这确实有效,我的代码中的其他地方有一个拼写错误false。这仍然将我带到我的链接。这确实有效。我的道歉,我上面的a评论在我代码的另一部分用flase拼写错误false。
var clickUpMouseEventTimer = 0; 

function clickUp(ev) {
   ev = ev || event;

   console.log('clickUp mouse timer' ,ev['timeStamp'], ev['timeStamp'] -clickUpMouseEventTimer)

   if( clickUpMouseEventTimer + 200 < ev['timeStamp']) {
       clickUpMouseEventTimer = ev['timeStamp'];
   } else {
       return;
   }