简体   繁体   English

Jquery在Chrome中工作,但不在Firefox中工作

[英]Jquery working in Chrome but not Firefox

I have the following jquery to stop any links from working. 我有以下jquery来阻止任何链接工作。 It works find in Chrome but fails in Firefox. 它可以在Chrome中找到但在Firefox中失败。 I don't see any errors. 我没有看到任何错误。 Per some information I found online I change the import to this: 根据我在网上找到的一些信息,我将导入更改为:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js" type="text/javascript"></script>

Here is the Jquery: 这是Jquery:

<script>
    var j$ = jQuery.noConflict();
    j$(document).ready(function (){
           stopClick();
    });


    function stopClick(){
        j$( ".cxfeeditem" ).on( "click", "a", function() {
                event.preventDefault();
         });
    }

  </script>

Thanks for any help, I'm really stuck. 谢谢你的帮助,我真的被困了。

You need to pass the event object as the first argument of the click handler. 您需要将事件对象作为单击处理程序的第一个参数传递。 Try this: 尝试这个:

function stopClick(){
    j$( ".cxfeeditem" ).on( "click", "a", function(event) {
            event.preventDefault();
     });
}

Symlink's answer is good, but he doesn't provide an explanation for why it works. Symlink的答案很好,但他没有解释它的工作原理。 Here's what's going on: 这是发生了什么:

In your original code, you had a button whose default action was presumably to submit the form. 在原始代码中,您有一个按钮,其默认操作可能是提交表单。 When you clicked the button, it went ahead and did that - ignoring the JavaScript that you'd written. 当您单击该按钮时,它会继续执行此操作 - 忽略您编写的JavaScript。

To make sure your JavaScript loads as expected, you must override the default action of the button. 要确保JavaScript按预期加载,您必须覆盖按钮的默认操作。 You do this by passing the event object into your function and invoking the preventDefault() method. 您可以通过将event对象传递给函数并调用preventDefault()方法来完成此操作。 This allows JavaScript can carry out the action you intended, rather than submitting the form as prescribed by the HTML. 这允许JavaScript可以执行您想要的操作,而不是按照HTML的规定提交表单。

Apparently FireFox is smart enough to know what you intended to do, but Chrome stubbornly maintained the HTML default. 显然,FireFox非常聪明,可以知道你打算做什么,但Chrome固执地维护了HTML的默认设置。

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

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