简体   繁体   English

e.preventDefault()不能在javascript中工作,但可以在jQuery中工作

[英]e.preventDefault() not working in javascript, but working in jQuery

This code should work fine, but i don't know where it is getting stuck! 这段代码应该可以正常工作,但是我不知道它在哪里卡住了!

This code is very simple, and it should work, I think the problem is the 3rd parameter that is passed ("mouseup", function(){}, false) 该代码非常简单,并且应该可以正常工作,我认为问题是传递的第三个参数(“ mouseup”,function(){},false)

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>html demo</title>
</head>
<body>
<a id="navlink" href="http://google.com">Click me</a>
<div id="Reveal">Not Clicked</div>
<script>
    document.getElementById('navlink').addEventListener("mouseup" , function(e){
        e.preventDefault();
        document.getElementById('reveal').innerHTML("Clicked");
    },false);
</script>

</body>
</html>

You have a problem with the innerHTML and the id of the div Reveal 您对innerHTML和div Reveal的id有问题

document.getElementById('navlink').addEventListener("mouseup" , function(e){
    e.preventDefault();
    document.getElementById('Reveal').innerHTML = "Clicked";
},false);

Regards. 问候。

Try this, 尝试这个,

document.getElementById('reveal').innerHTML = "Clicked";

Because innerHTML is property not a function. 因为innerHTML是属性而不是函数。 Also you have a typo in your code, 'Reveal' is the id of DIV. 另外,您的代码中有错别字,“显示”是DIV的ID。

You need the following corrections, please see the snippet below 您需要进行以下更正,请参见下面的代码段

1) .innerHtml is not a method, its a property so you must use = 1) .innerHtml不是方法,它是属性,因此必须使用=

2) getElementById are case sensitive so document.getElementById('reveal') must be document.getElementById('Reveal') 2) getElementById区分大小写,因此document.getElementById('reveal')必须为document.getElementById('Reveal')

 document.getElementById('navlink').addEventListener("mouseup" , function(e){ e.preventDefault(); document.getElementById('Reveal').innerHTML = "Clicked"; },false); 
 <a id="navlink" href="http://google.com">Click me</a> <div id="Reveal">Not Clicked</div> 

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors 参考: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_Accessors

Please try adding click event instead of mouseup. 请尝试添加点击事件,而不是mouseup。

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>html demo</title>
  </head>
  <body>
    <a id="navlink" href="http://google.com">Click me</a>
      <div id="Reveal">Not Clicked</div>
    <script>
      document.getElementById('navlink').addEventListener("click" , function(e){
      e.preventDefault();
      //document.getElementById('reveal').innerHTML("Clicked");
      },false);
    </script>
</body>
</html>

innerHTML is a property , not a mutator method. innerHTML属性 ,而不是mutator方法。 The syntax are as follows: 语法如下:

Return the innerHTML property: HTMLElementObject.innerHTML 返回 innerHTML属性: HTMLElementObject.innerHTML

Set the innerHTML property: HTMLElementObject.innerHTML=text 设置 innerHTML属性: HTMLElementObject.innerHTML=text

In your case, it should be: document.getElementById('reveal').innerHTML = "Clicked"; 在您的情况下,应为: document.getElementById('reveal').innerHTML = "Clicked";

Also, the last parameter for the addEventListener is optional. 另外, addEventListener的最后一个参数是可选的。 It is a Boolean value that specifies whether the event should be executed in the capturing or in the bubbling phase. 它是一个布尔值,它指定是在捕获阶段还是冒泡阶段执行事件。

e.preventDefault does not prevent either mouseup or mousedown . e.preventDefault不会阻止mouseupmousedown Check this answer. 检查答案。 You must use click() or addEventListener("click", to use preventDefault . 您必须使用click()addEventListener("click",才能使用preventDefault

Example: document.getElementById('navlink').addEventListener("click" , function(e){ 范例: document.getElementById('navlink').addEventListener("click" , function(e){

And in your code you made a mistake of using innerHTML as a function and reveal has a capital R, so the corrected form is 在您的代码中,您使用innerHTML作为函数时犯了一个错误,并且reveal具有大写的R,因此更正的形式是

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>html demo</title>
</head>
<body>
<a id="navlink" href="http://google.com">Click me</a>
<div id="Reveal">Not Clicked</div>
<script>
    document.getElementById('navlink').addEventListener("click" , function(e){
        e.preventDefault();
        document.getElementById('Reveal').innerHTML = "Clicked";
    },false);
</script>

</body>
</html>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM