javascript 无法在“窗口”上执行“getComputedStyle”:参数 1 的类型不是“元素”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30279967/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:59:18  来源:igfitidea点击:

Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element' error

javascriptjquery

提问by bhttoan

I am having a problem with some inherited code - it is a wall app something like FB where registered users can post topics. A lot of the code is JS and jQuery and I know little about either.

我遇到了一些继承代码的问题 - 它是一个类似于 FB 的墙应用程序,注册用户可以在其中发布主题。很多代码是 JS 和 jQuery,我对这两者知之甚少。

When posting a topic the topic gets added to the database but the screen does not show the topic until it is refreshed but it should show immediately - when I look in Developer Tools I get the error:

发布主题时,该主题会添加到数据库中,但屏幕在刷新之前不会显示该主题,但应立即显示 - 当我查看开发人员工具时,出现错误:

Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.

When I expand the error I get:

当我扩展错误时,我得到:

curCSS  @   jquery-1.8.3.js:6825
jQuery.extend.css   @   jquery-1.8.3.js:6782
isHidden    @   jquery-1.8.3.js:6587
defaultPrefilter    @   jquery-1.8.3.js:8797
Animation   @   jquery-1.8.3.js:8697
doAnimation @   jquery-1.8.3.js:9034
jQuery.extend.dequeue   @   jquery-1.8.3.js:1895
(anonymous function)    @   jquery-1.8.3.js:1938
jQuery.extend.each  @   jquery-1.8.3.js:611
jQuery.fn.jQuery.each   @   jquery-1.8.3.js:241
jQuery.fn.extend.queue  @   jquery-1.8.3.js:1931
jQuery.fn.extend.animate    @   jquery-1.8.3.js:9044
jQuery.fn.(anonymous function)  @   jquery-1.8.3.js:9129
(anonymous function)    @   script.js:52
fire    @   jquery-1.8.3.js:974
self.fireWith   @   jquery-1.8.3.js:1084
done    @   jquery-1.8.3.js:7803
callback    @   jquery-1.8.3.js:8518

Line 52 in script.js is:

script.js 中的第 52 行是:

$('#loadpage').prepend($(response).fadeIn('slow'));

I am hoping this isn't something fixed in jQuery 1.9.x and above as upgrading breaks too many things at the moment to make it viable but I have no idea where to begin troubleshooting this

我希望这不是在 jQuery 1.9.x 及更高版本中修复的问题,因为目前升级破坏了太多东西以使其可行,但我不知道从哪里开始解决这个问题

I found three questions with the same error but wasn't able to get any ideas from the answers probably due to mylack of JS knowledge.

我发现了三个具有相同错误的问题,但由于我缺乏 JS 知识,我无法从答案中得到任何想法。

Have made some progress - I updated to jQUery 1.9.1 and this time the same happened but the error changed. This time the error became:

取得了一些进展 - 我更新到 jQUery 1.9.1,这次发生了同样的情况,但错误发生了变化。这次错误变成了:

Uncaught TypeError: Cannot set property 'cur' of undefined

pointing to the same line in script.js

指向 script.js 中的同一行

A quick search found Uncaught TypeError with fadeIn in jQuery 1.9which recommended changing the line:

快速搜索在 jQuery 1.9 中发现Uncaught TypeError 和fadeIn,建议更改该行:

$('#loadpage').prepend($(response).fadeIn('slow'));

to:

到:

$('#loadpage').prepend($(response).show());

When I do this there are no longer any errors but it still does not quite work correctly - when the new post is posted there is a new entry added to the list but it is a duplicate of the previous post. So for example,

当我这样做时,不再有任何错误,但它仍然不能正常工作 - 当发布新帖子时,列表中添加了一个新条目,但它是上一个帖子的副本。例如,

Post 1
Post 2
Post 3

If I post a new post called Post 4 then it displays as:

如果我发布了一个名为 Post 4 的新帖子,它会显示为:

Post 1
Post 1
Post 2
Post 3

When I refresh it then shows correctly as Post 4 etc so feels like some progress but still not quite there

当我刷新它时,它会正确显示为 Post 4 等,所以感觉有些进步,但仍然不完全

Response is defined in:

响应定义在:

    $('#post').click(function(){
        var a = $("#wm").val();
        if(a != "")
        {
            var keepID = $('#keepID').val();
            var posted_on = $('#posted_on').val();

            $.post("wall.php?value="+a+'&x='+keepID+'&p='+posted_on, {
            }, function(response){
                //console.log(response);        

                //$('#load').prepend($(response).fadeIn('slow'));
                $('#load').prepend($(response).show());
                $("#wm").val("");
            });
        }


    });

This script is fricking horrific!

这个剧本太可怕了!

回答by Kriegslustig

You'll probably need to insert the element before showing it. Try this:

您可能需要在显示之前插入该元素。试试这个:

$('#post').click(function(){
    var a = $("#wm").val();
    if(a != "")
    {
        var keepID = $('#keepID').val();
        var posted_on = $('#posted_on').val();

        $.post("wall.php?value="+a+'&x='+keepID+'&p='+posted_on, {
        }, function(response){
            $(response).prependTo($('#load')).fadeIn('slow');
            $("#wm").val("");
        });
    }
});

If that doesn't work, there must be an error in the php scriptor in the GET params you are passing

如果这不起作用,则php 脚本您传递的 GET 参数中肯定有错误