Javascript IE 错误:意外调用方法或属性访问

发布于 2024-08-23 18:15:39 字数 1561 浏览 1 评论 0 原文

我有以下代码,它在除 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 等中运行良好。

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).

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

And here is the demo I have been following:
http://plugins.jquery.com/project/history

Still changing window.location back to document.location doesn't seem to make a difference

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

红玫瑰 2024-08-30 18:15:39

我对 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 for hash (e.g., put var in front of the first use).

On the others it's presumably creating an implicit global (a property of the window object 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"]).

Still surprised IE is complaining about it, though. It must have to do with the scope in which jQuery is calling your click handler.

吃不饱 2024-08-30 18:15:39

如果在 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.

风柔一江水 2024-08-30 18:15:39

它将是:

$('tbody').html(html);

当您尝试将某些内容放入某些节点时,IE 会抛出错误。例如,

$('<input>').append('</p>')

要解决此问题,请将 html 添加到不同的元素。例如,您可以尝试:

$('table').html('<tbody>' + html + '</tbody>');

It will be the:

$('tbody').html(html);

IE throws an error when you try to put certain things inside some nodes. Eg

$('<input>').append('</p>')

To fix this, add the html to a different element. For example, you could try:

$('table').html('<tbody>' + html + '</tbody>');
╄→承喏 2024-08-30 18:15:39

我在 jQuery API 中找不到历史方法,所以我认为它是:

  • 第三方插件。
  • 间接调用旧的 window.history 对象。
  • 您创建的自定义对象。

我的猜测是,IE(以通过使所有内容全球化而污染全局范围而闻名)认为任何对 history 的引用实际上都意味着 window.history 并且它变得混乱。如果它是自定义对象,请尝试将其重命名为 myHistory 或其他任何名称。

I can't find a history method in the jQuery API so I presume it's either:

  • A third-party plugin.
  • An indirect call to good old window.history object.
  • A custom object you created.

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.

林空鹿饮溪 2024-08-30 18:15:39

我认为克劳德先生说得有道理。据我从 jQuery 源代码得知,如果您返回的 HTML 片段不包含任何

您可能需要做的是将整个 包装在虚拟

中,然后从 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.

蓝梦月影 2024-08-30 18:15:39

我在使用 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.

转身泪倾城 2024-08-30 18:15:39

我也有同样的问题。最初有一个选择列表被附加到其中。在此过程中,选择更改为输入字段。尝试将选项标签附加到输入字段似乎会让 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.

攒一口袋星星 2024-08-30 18:15:39

我现在也有同样的问题。在我看来,这种情况发生在不可见元素上。验证您的元素在调用 .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.

白衬杉格子梦 2024-08-30 18:15:39

您是否设置任何输入字段?

我遇到了同样的问题,循环访问值数组,但我已将文本框更改为输入。 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 input

Already had the val call, but needed to remove the html one.

$("#someID").val("someValue") = good

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文