Javascript IE 错误:对方法或属性访问的意外调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2350554/
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
Javascript IE error: unexpected call to method or property access
提问by Ke.
I have the following code and it's working (as usual) in everything but IE. It's giving me an unexpected call to method or property access in Jquery and I have no idea how to debug it. I've been using the IE developer toolbar, which is useless for this error and just gives me a line no 12 (inside the jquery script).
我有以下代码,它在除 IE 之外的所有内容中都可以正常工作(像往常一样)。它给了我对 Jquery 中方法或属性访问的意外调用,我不知道如何调试它。我一直在使用 IE 开发人员工具栏,这对这个错误没有用,只是给了我一个没有 12 的行(在 jquery 脚本中)。
Any help is v much appreciated:
非常感谢任何帮助:
<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>
Here is the history plugin: http://plugins.jquery.com/project/history
这是历史插件:http: //plugins.jquery.com/project/history
And here is the demo I have been following: http://plugins.jquery.com/project/history
这是我一直在关注的演示:http: //plugins.jquery.com/project/history
Still changing window.location back to document.location doesn't seem to make a difference
仍然将 window.location 改回 document.location 似乎没有什么区别
I'm lost on this one. When I change the tag I'm calling to it does post so it's working, but in IE the design is all broken and the next links I click on don't post. Really strange!! Works fine in firefox, opera, etc.
我迷失在这一点上。当我更改标签时,我调用它确实可以发布,因此它可以正常工作,但是在 IE 中,设计已全部损坏,我单击的下一个链接不发布。真的很奇怪!!在 Firefox、Opera 等中运行良好。
采纳答案by T.J. Crowder
I'm a bit surprised IE complains about it, but it's a good thing it does: You're missing a declaration in getPagefor hash(e.g., put varin front of the first use).
我有点惊讶 IE 会抱怨它,但它确实是一件好事:您缺少getPagefor 中的声明hash(例如,放在var第一次使用之前)。
On the others it's presumably creating an implicit global (a property of the windowobject called hash), 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 Reference
Type"] and 10.1.4 ["Scope Chain and Identifier Resolution"]).
在其他方面,它大概创建了一个隐式全局(window称为的对象的属性hash),这当然是坏事(tm),但据我所知,它根据规范是正确的(相关部分为 8.7 [“参考类型”] 和 10.1.4 [“作用域链和标识符解析”])。
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 仍然对此抱怨感到惊讶。它必须与 jQuery 调用单击处理程序的范围有关。
回答by Tamlyn
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 中使用 HTML5 元素时遇到此错误,请确保使用 html5shim 告诉 IE 这些是真实元素。
回答by dave1010
It will be the:
它将是:
$('tbody').html(html);
IE throws an error when you try to put certain things inside some nodes. Eg
当您尝试将某些内容放入某些节点时,IE 会引发错误。例如
$('<input>').append('</p>')
To fix this, add the htmlto a different element. For example, you could try:
要解决此问题,请将 添加html到不同的元素。例如,您可以尝试:
$('table').html('<tbody>' + html + '</tbody>');
回答by Sam
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.
我在使用 HTML5 元素的 Web 应用程序中遇到了这个问题。当我将 Modernizr 脚本包含到页面中时,问题就解决了。
回答by jhanifen
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.
我有同样的问题。最初有一个选择列表被附加到它。一路上,选择已更改为输入字段。尝试将选项标签附加到输入字段似乎让 IE 不高兴。
回答by Jovica Zaric
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.
我现在遇到了同样的问题。在我看来,它发生在不可见元素上。验证您的元素在调用.html()它时是否可见。
回答by MattC
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.
我有同样的问题,循环遍历一组值,但我已将文本框更改为输入。Chrome 只是忽略它,但 IE 8 爆炸了。
$("#someID").html("someValue")= boom! on IE 8 if someID is an input
$("#someID").html("someValue")= 繁荣!如果 someID 是输入,则在 IE 8 上
Already had the val call, but needed to remove the html one.
已经有 val 调用,但需要删除 html 调用。
$("#someID").val("someValue")= good
$("#someID").val("someValue")= 好
回答by álvaro González
I can't find a history method in the jQuery APIso I presume it's either:
我在jQuery API 中找不到历史方法,所以我认为它是:
- A third-party plugin.
- An indirect call to good old window.historyobject.
- A custom object you created.
- 第三方插件。
- 对旧的window.history对象的间接调用。
- 您创建的自定义对象。
My guess is that IE (well known for polluting the global scope by making everything global) thinks any reference to historyactually means window.historyand it's getting confused. If it's a custom object, try to rename it into myHistoryor anything else.
我的猜测是 IE(众所周知,通过将所有内容都设为全局来污染全局范围)认为任何对history 的引用实际上都意味着window.history并且它变得混乱了。如果它是自定义对象,请尝试将其重命名为myHistory或其他任何内容。
回答by Pointy
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.
我认为克劳德先生正在做某事。据我从 jQuery 源代码中得知,如果您返回的 HTML 片段不包含任何<script>标签,并且没有其他一些不太可能的特征,那么 jQuery 可能正试图将其填充到您的<tbody>元素中通过使用“innerHTML”属性。IE 不喜欢那样。
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.
您可能需要做的是将整个内容包装<table>在一个虚拟对象中<div>,然后从您的 HTML 片段中重建整个内容。

