Javascript jquery .click() event.preventDefault() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27870794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:49:35  来源:igfitidea点击:

jquery .click() event.preventDefault() not working

javascriptjquery

提问by KingRichard

I'm building a web application that is extendable by dropping scripts into a preset directory. The script needs to read file headers before going to a page, so I need to use .preventDefault() when links are clicked and run some JScript first. Unfortunately I can't get past the .preventDefault()

我正在构建一个可通过将脚本放入预设目录来扩展的 Web 应用程序。该脚本需要在进入页面之前读取文件标题,因此我需要在单击链接时使用 .preventDefault() 并首先运行一些 JScript。不幸的是,我无法通过 .preventDefault()

Here's my link format:

这是我的链接格式:

<a href="path/to/file.php" class="filelink">Some Text</a>

here's the jquery I'm trying to use to stop the default action:

这是我试图用来停止默认操作的 jquery:

    $( '.filelink' ).click( function( e ) {
        e.preventDefault();
        //do some other stuff here
    });

EDIT:

编辑:

More Info:

更多信息:

The <script>...</script>is in my footer. This function is INSIDE $(document).readyAFTER a $.ajaxcall that builds the list of links this function is trying to listen for clicks of.

<script>...</script>是我的页脚。此函数是$(document).ready$.ajax调用构建此函数试图侦听点击的链接列表之后的INSIDE 。

回答by r3wt

Since your links are appended dynamically to the page, you need to use document.on()to capture the click events.

由于您的链接是动态附加到页面的,因此您需要使用document.on()来捕获点击事件。

the syntax for appending event listeners to dynamic content is as follows

将事件侦听器附加到动态内容的语法如下

$(document).on( event, selector, callback );

$(document).on( event, selector, callback );

so your click handler would become:

所以你的点击处理程序将变成:

$(document).on('click','.filelink',function(e){

 e.preventDefault();

 //code here

 return false;

});

回答by Mouhamad Kawas

I think you missed $(document).ready()

我想你错过了 $(document).ready()

Please update your code as following:

请按以下方式更新您的代码:

$(document).ready(function(){
     $( '.filelink' ).click( function( e ) {
        e.preventDefault();
        //do some other stuff here
    });

})

回答by Santiago Hernández

is your code surrounded by:

您的代码是否被以下内容包围:

$(function(){

//your code here

});

? or

? 或者

$(document).ready(function(){

//your code here

});

? , because you should wait until the <a></a>element is ready before adding your click listener to it. try the code above or place your code at the bottom of the html right before the </body>tag or just right after the <a></a>element. that should work

? ,因为您应该等到<a></a>元素准备好后再添加点击侦听器。尝试上面的代码或将您的代码放在 html 底部的</body>标记之前或<a></a>元素之后。那应该工作

EDIT:since your links are added dynamically call your $( '.filelink' ).click()function right after they are added

编辑:由于您的链接是动态$( '.filelink' ).click()添加的,因此在添加后立即调用您的函数

回答by arthur.sw

I had the same problem, because I changed the window.location.search in my event handler:

我遇到了同样的问题,因为我在事件处理程序中更改了 window.location.search:

 $( '.filelink' ).click( function( e ) {
        e.preventDefault();
       window.location.search = "something";
 });

To fix my problem I used window.location.hash(which is more what I wanted anyway).

为了解决我使用的问题window.location.hash(无论如何,这更像是我想要的)。

回答by manian

For me, the issue was a syntax error in the Javascript code.

对我来说,问题是 Javascript 代码中的语法错误。

I substituted a php variable to one of the JS variable like below,

我将一个 php 变量替换为如下所示的 JS 变量之一,

var myvar = "<?php echo $myvar; ?>";

var myvar = "<?php echo $myvar; ?>";

which produced the below result,

产生了以下结果,

var myvar = "value in "myvar" which is not escaped";

var myvar = "value in "myvar" which is not escaped";

It worked when I escaped the variable.

当我转义变量时它起作用了。

Please check if there is any syntax error in your code.

请检查您的代码中是否有任何语法错误。