javascript JQuery 在 Firefox 中不起作用,但在 Chrome 中有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34286460/
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
JQuery does not work in Firefox but works in Chrome
提问by dj.milojevic
I'm having trouble with jQuery and Mozzila Firefox. Everything is working just fine in Chrome, but somehow Firefox does not see jQuery.
我在使用 jQuery 和 Mozzila Firefox 时遇到了问题。在 Chrome 中一切正常,但不知何故 Firefox 看不到 jQuery。
This is how I call jQuery
这就是我调用 jQuery 的方式
<!-- Favicon and touch icons -->
<link rel="shortcut icon" href="assets/ico/favicon.png">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script type="text/javascript" src="assets/bootstrap/js/datepicker.js"></script>
And this is where it fails (error is: ReferenceError: $ is not defined):
这就是它失败的地方(错误是:ReferenceError: $ is not defined):
<script>
function ajax_check(){
var id = $("#xml_select").val(); // this is the line where I get error
$.ajax({
url: "ajax_check.php?id="+id,
success: function(response) {
var result = jQuery.parseJSON(response);
//console.log( JSON.stringify(result['ncp'].replace('"','')) );
var ncp = JSON.stringify(result['ncp']);
var id = JSON.stringify(result['id']);
$("#racun").val(ncp.substring(1,12));
$("#id_podnosilac").val(id.substring(1,5));
},
});
}
</script>
Please help, what could be causing this?
请帮忙,这可能是什么原因造成的?
采纳答案by dj.milojevic
回答by Thanga
You will get this error randomly based on loading time/different browsers. Because Root cause of this is that you are loading jquery.min.js from googleapis. Third party domain resources will get low priority than the local domain resources. "document ready" function statements will be triggered once the local domain resources are loaded. That is why you get this error.
您将根据加载时间/不同浏览器随机收到此错误。因为根本原因是您从 googleapis 加载 jquery.min.js。第三方域资源将获得比本地域资源低的优先级。一旦加载了本地域资源,就会触发“文档就绪”函数语句。这就是您收到此错误的原因。
Permanent Solution: Put jquery.min.js file in your server and call it from your domain. this solution will work even if your page has load time issues and in any browser.
永久解决方案:将 jquery.min.js 文件放在您的服务器中并从您的域中调用它。即使您的页面存在加载时间问题并且在任何浏览器中,此解决方案也将起作用。
回答by Omid Ahmadyani
use "jQuery" word (with out qutation) instead of $ in your code
在您的代码中使用“jQuery”词(不带引号)而不是 $
回答by lesolorzanov
This problem persists even 3 years later. some people use jquery just for the $ function which is ridiculous. One could program it oneself.
这个问题甚至在 3 年后仍然存在。有些人只将 jquery 用于 $ 函数,这很荒谬。可以自己编程。
$ = document.getElementById
or ByClass
there are many ways to search within the DOM and new ones that are appearing.
$ = document.getElementById
或者ByClass
有很多方法可以在 DOM 中搜索以及出现的新方法。
I had the problem of assigning functions for when the document was loaded which was solved by this question here
我在加载文档时遇到了分配函数的问题,此问题已在此处解决