jQuery 的 .load() 在 Chrome 浏览器中无法正常工作
我已经测试过它,它可以在 Opera、Safair、IE 和 Firefox 中运行。我读过其他几篇 stackoverflow 帖子和网页条目来解释这个问题,最常见的答案是将其放在实时服务器上,因为 chrome 不接受本地 ajax 函数。所以我将代码上传到我的 godaddy 服务器上,但它仍然无法工作,或者我是否误解了“服务器”是什么?我认为服务器只是将其上传到我的付费网络托管帐户。
我发现的第二个最常见的答案是使用“--allow-file-access-from-files”参数从终端启动 Chrome。当我尝试创建一个其他 Chrome 用户在访问我的网站时可以访问的应用程序时,我到底该怎么做?这是一个可行的解决方案吗?
我正在使用的相关代码片段如下,如果它与这个问题相关的话:(基本上,高度返回为0,因为图像尚未加载。这个问题不会出现在任何其他浏览器中,我已经测试过这也在我的实时网站上)
$("img").click(function() {
$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
$("#galleryImg img").load(function() {
var imgHeight = $(this).height();
alert(imgHeight);
});
});
I've tested it and it works in opera, safair, IE, and firefox. I've read several other stackoverflow posts and web entries explaining the problem and the most common answer has been to put it on a live server as chrome doesn't accept ajax functions locally. So I uploaded my code onto my godaddy server and it's still not working, or am I mis-understanding what a 'server' is? I thought servers simply meant upload it to my paid web hosting account.
The second most common answer I found was to launch Chrome from Terminal with "--allow-file-access-from-files" argument. How exactly do I go about doing this, and this is a working solution when I'm trying to make an application that other chrome users can access while visiting my website?
The relevant snippet of code that I am using is as follows, if it's relevant to this question: (basically, the height returns as 0 because the image has not loaded. This problem does not occur in any of the other browsers and I have tested this on my live website as well)
$("img").click(function() {
$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
$("#galleryImg img").load(function() {
var imgHeight = $(this).height();
alert(imgHeight);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
基本上,jQuery 知道它不能在所有浏览器中工作,并且并不真正支持它:
由 load- 表示事件文档:
Basically, jQuery knows it doesn't work in all browsers and doesn't really support it:
Said By load-event docs:
.load 不适用于图像。查看这个插件 - https://gist.github.com/268257
.load doesnt work on images. CHeck out this plugin - https://gist.github.com/268257
你缺少一个右大括号并且没有指定 url
you're missing a closing brace and you're not specifying a url
也许当您尝试绑定它时该事件已经触发?
试试这个:
Maybe the event has already fired when you try to bind it?
Try this:
您在此处调用的 .load() 是“绑定到加载事件”之一,而不是 ajax 之一。 load-event 与 load-data
编辑以回答您的评论。这就是我要做的。
然后处理 click 事件 - 通过调用 html 函数,由于您之前的绑定,将触发 load 事件。
The .load() you're calling here is "bind to the load event" one, not the ajax one. load-event vs load-data
Edit to answer your comment. Here's what I would do.
Then handle the click event - by calling the html function, the load event will fire because of your previous binding.
我们提出的解决方案涉及使用纯 JavaScript 代替 jQuery().load() 函数。
假设您有一个目标元素,您想要从其他 HTML 文件插入您的 HTML 内容,您可以使用此解决方案:
1) 为每个 HTML 页面创建单独的 JS 文件,并在其中插入简单的字符串变量,并将您的所有内容作为其 内容价值。
[ 示例:content1.html -> contant1.js]...
{
var myContent1 = 'Hello, World!'
}2) 在您的母版页中包含所有内容 js 文件。
[ 示例:
]...
3) 要将您的内容插入到母版页中,请使用简单的 DOM 属性(如innerHTML):
[ 示例:
onClick = document.getElementById('target_Element').innerHTML = myContent1;
]作者:mah3 &威楚1
Solution we came up with involves using plain JavaScript insted of jQuery().load() function.
Assuming You have a target element to which You want to insert Your HTML content from other HTML files You can use this solution:
1) For each HTML page create separate JS file and inside of it insert just simple string variable with all Your contant as its value.
[ example: content1.html -> contant1.js ]...
{
var myContent1 = 'Hello, World!'
}2) In Your master page include all content js files.
[ example:
<script src="content1.js"></script>
]...3) To insert Your content to a master page use simple DOM property like innerHTML:
[ example:
onClick = document.getElementById('target_Element').innerHTML = myContent1;
]Authors: mah3 & wichu1