Javascript IE 错误:意外调用方法或属性访问
我有以下代码,它在除 IE 之外的所有版本中都可以正常工作(像往常一样)。它给了我一个对 Jquery 中方法或属性访问的意外调用,我不知道如何调试它。我一直在使用 IE 开发工具栏,它对这个错误没有用,只是给了我第 12 行(在 jquery 脚本内)。
非常感谢任何帮助:
<script type="text/javascript">
$(document).ready(function () {
$.history.init(pageload);
$('a[href=' + window.location.hash + ']').addClass('selected');
$('a[rel=ajax]').click(function () {
var hash = this.href;
hash = hash.replace(/^.*#/, '');
$.history.load(hash);
$('a[rel=ajax]').removeClass('selected');
$(this).addClass('selected');
$('.loading').show();
getPage();
return false;
});
});
function pageload(hash) {
if (hash) getPage();
}
function getPage() {
hash = document.location.hash;
hash = hash.replace(/^.*#/, '');
var data = 'page=' + encodeURIComponent(hash);
$.ajax({
url: "index.php",
type: "POST",
data: data,
cache: false,
success: function (html) {
$('.loading').hide();
$('tbody').html(html);
}
});
}
</script>
这是历史记录插件: http://plugins.jquery.com/project /history
这是我一直在关注的演示: http://plugins.jquery.com/project/history
仍在将 window.location 更改回document.location 似乎没有什么区别,
我对此迷失了。当我更改标签时,我调用的标签确实会发布,因此它可以工作,但在 IE 中,设计全部损坏,我点击的下一个链接不会发布。真的很奇怪!!在 firefox、opera 等中运行良好。
发布评论
评论(9)
我对 IE 的抱怨感到有点惊讶,但它确实做了一件好事:您在
getPage
中缺少hash
的声明(例如,将var
在第一次使用前面)。在其他情况下,它可能会创建一个隐式全局变量(
window
对象的一个名为hash
的属性),这当然是一个 hash。 niftysnippets.org/2008/03/horror-of-implicit-globals.html" rel="noreferrer">坏事(tm),但据我了解,根据规范,它是正确的(相关部分是8.7 [“参考资料Type"] 和 10.1.4 ["作用域链和标识符解析"])。
尽管如此,IE 仍然对此抱怨感到惊讶。它一定与 jQuery 调用您的单击处理程序的作用域有关。
I'm a bit surprised IE complains about it, but it's a good thing it does: You're missing a declaration in
getPage
forhash
(e.g., putvar
in front of the first use).On the others it's presumably creating an implicit global (a property of the
window
object calledhash
), which is of course a Bad Thing(tm), but as I understand it it's correct according to the specification (the relevant sections being 8.7 ["The ReferenceType"] and 10.1.4 ["Scope Chain and Identifier Resolution"]).
Still surprised IE is complaining about it, though. It must have to do with the scope in which jQuery is calling your click handler.
如果在 IE 中使用 HTML5 元素时出现此错误,请确保使用 html5shim 告诉 IE 这些是真实元素。
IF you get this error when using HTML5 elements in IE, make sure you're using html5shim to tell IE that these are real elements.
它将是:
当您尝试将某些内容放入某些节点时,IE 会抛出错误。例如,
要解决此问题,请将
html
添加到不同的元素。例如,您可以尝试:It will be the:
IE throws an error when you try to put certain things inside some nodes. Eg
To fix this, add the
html
to a different element. For example, you could try:我在 jQuery API 中找不到历史方法,所以我认为它是:
我的猜测是,IE(以通过使所有内容全球化而污染全局范围而闻名)认为任何对 history 的引用实际上都意味着 window.history 并且它变得混乱。如果它是自定义对象,请尝试将其重命名为 myHistory 或其他任何名称。
I can't find a history method in the jQuery API so I presume it's either:
My guess is that IE (well known for polluting the global scope by making everything global) thinks any reference to history actually means window.history and it's getting confused. If it's a custom object, try to rename it into myHistory or anything else.
我认为克劳德先生说得有道理。据我从 jQuery 源代码得知,如果您返回的 HTML 片段不包含任何
元素中。 IE 不喜欢这样。标记,并且不具有其他一些不太可能的特征,那么 jQuery 可能会尝试使用“innerHTML”属性将其填充到您的
您可能需要做的是将整个
包装在虚拟
中,然后从 HTML 片段重建整个内容。
I think that Mr. Crowder is on to something. As far as I can tell from the jQuery source, if your returned HTML fragment doesn't contain any
<script>
tags, and doesn't have a couple of other not-too-likely characteristics, then jQuery is probably trying to stuff it into your<tbody>
element by using the "innerHTML" property. IE doesn't like that.What you might have to do is wrap the whole
<table>
in a dummy<div>
and then rebuild the entire thing from your HTML fragment.我在使用 HTML5 元素的 Web 应用程序中遇到了这个问题。当我将 Modernizr 脚本包含到页面中时,问题就解决了。
I had this problem in a web application that used HTML5 elements. When I included the Modernizr script into the pages, the problem was solved.
我也有同样的问题。最初有一个选择列表被附加到其中。在此过程中,选择更改为输入字段。尝试将选项标签附加到输入字段似乎会让 IE 不高兴。
I had the same problem. Originally there was a select list that was getting an appended to it. Along the way the select was changed to an input field. Trying to append an option tag to an input field seem to make IE unhappy.
我现在也有同样的问题。在我看来,这种情况发生在不可见元素上。验证您的元素在调用
.html()
时是否可见。I had the same problem right now. It seems to me that it happens for non-visible elements. Verify if your element is visible at the moment of calling
.html()
on it.您是否设置任何输入字段?
我遇到了同样的问题,循环访问值数组,但我已将文本框更改为输入。 Chrome 直接忽略它,但 IE 8 却爆炸了。
$("#someID").html("someValue")
= 繁荣!在 IE 8 上,如果 someID 是一个输入已经有 val 调用,但需要删除 html 调用。
$("#someID").val("someValue")
= 好Are you setting any input fields?
I had the same issue, looping through an array of values, but I had changed a text box to an input. Chrome just ignores it, but IE 8 blows up.
$("#someID").html("someValue")
= boom! on IE 8 if someID is an inputAlready had the val call, but needed to remove the html one.
$("#someID").val("someValue")
= good绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
关于作者
每个人心里都住着一个人,或眷念,或暗恋,或想念。
相关话题
热门标签
推荐作者
qq_E2Iff7
文章 0 评论 0
Archangel
文章 0 评论 0
freedog
文章 0 评论 0
Hunk
文章 0 评论 0
18819270189
文章 0 评论 0
wenkai
文章 0 评论 0
友情链接
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。