📜  preventDefault() 事件方法

📅  最后修改于: 2021-11-24 04:42:02             🧑  作者: Mango

preventDefault()方法用于阻止浏览器执行所选元素的默认操作。它可以阻止用户通过单击链接来处理请求。
句法:

event.preventDefault()

参数:它不接受任何参数。
事件用于表示用户响应该方法起作用的事件或动作。

显示 preventDefault()函数工作的 jQuery 代码:

代码#1:
此代码将阻止浏览器转到另一个页面。


      
            GEEKSFORGEEKS ARTICLE<title/>
                <!-- INCLUDE THE JQUERY CDN -->
                <script src="https://ajax.googleapis.com/ajax/
                libs/jquery/3.3.1/jquery.min.js"></script>
            <script type="text/javascript">
               $(document).ready(function()
                {    
              $("a").click(function(event)
               {            
                event.preventDefault();
                alert("prevented");
               });
                });
             </script>
    </head>
    <body>
        <p>Click the link:</p>
           <a href="https://www.google.com">GOOGLE</a>
    </body>
</html>
</code></pre>
</div>
<p><strong>输出:</strong><br/>它显示了如下所示的输出- <br/><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/preventDefault()_Event_Method_0.jpg"/><br/><strong>代码#2:</strong><br/>此代码将阻止浏览器将页面重定向到另一个PHP文件。 </p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-js" data-lang="JavaScript"><code class="replace"><html>
         <head>
             <title>GEEKSFORGEEKS ARTICLE<title/>
        <script src="https://ajax.googleapis.com/ajax/
             libs/jquery/3.3.1/jquery.min.js"></script>
           <script type="text/javascript">
              $(document).ready(function()
               {    
              $("#submit").click(function(event)
               {            
                event.preventDefault();
                alert("ACTION IS PREVENTED");
                });
                });
            </script>
    </head>
    <body>
       <form action="submit.php">
            <input type="text" placeholder="enter username">
            <input id="submit" type="submit" name="submit">
       </form>
    </body>
</html>
</code></pre>
</div>
<p><strong>输出:</strong><br/>它显示了如下所示的输出- <br/><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/preventDefault()_Event_Method_1.jpg"/></p>
<div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7" style="min-height:280px"></div>
<p></p></div>
</article>

                </div>

            </div>
        </div>
    </div>
</div>


  
<footer>
    <div class="bg-white text-center pb-1">
        <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有
            <br>

            <a href="https://beian.miit.gov.cn/" target="_blank"
                class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a>

            <br>
            Made with ❤️ in Chengdu
        </p>
    </div>
</footer>


  <!-- 引入Bootstrap JavaScript库 -->
  <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
    crossorigin="anonymous"></script>

  <!-- 引入Meilisearch搜索相关的JavaScript库 -->
  <script
    src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
  <script src="https://imangodoc.com/static/javascript/meili_custom.js"></script>

  <!-- 引入prismjs代码高亮相关的JavaScript库 -->
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
  <script
    src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>

</body>


</html>