jQuery:未捕获的类型错误:无法读取未定义的属性“nodeType”

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

jQuery: Uncaught TypeError: Cannot read property 'nodeType' of undefined

jqueryjquery-ui

提问by Emerson Maningo

I use jQUery UI Position plugin: http://jqueryui.com/position/to position my icons on a web page. The selectors are grabbed from the database and outputted to JS using PHP in a $myselector variable. This is my current code:

我使用 jQUEry UI Position 插件:http: //jqueryui.com/position/在网页上定位我的图标。选择器从数据库中获取并使用 PHP 在 $myselector 变量中输出到 JS。这是我当前的代码:

var element_selector='<?php echo $myselector;?>';

$('#inline_docxdiv .Featured.Slider').position({
my: "center",
at: "right top",
of: $(element_selector)

});

//append icons,applicable to all

$(divname<?php echo $uniqueid;?>).append('<div id="inline_docxdiv" class="<?php echo $uniqueid;?>"><div id="helpericons_display"><a class="<?php echo $title_toolsetdisplayed;?>" id="questionmarkicon_inlinedoc" title="Display Explanation"><img src="<?php echo $helper_iconpng;?>"></a><a target="_blank" href="<?php echo admin_url().'post.php?post='.$id_toolsetdisplayed.'&action=edit';?>" class="<?php echo $title_toolsetdisplayed;?>" id="sourceicon_inlinedoc" title="View source"><img src="<?php echo $helpersource_iconpng;?>"></a></div></div>');

However the icons are not appended correctly and it returns an error in the console:

但是图标没有正确附加,它在控制台中返回一个错误:

Uncaught TypeError: Cannot read property 'nodeType' of undefined

未捕获的类型错误:无法读取未定义的属性“nodeType”

The strange thing is that if I hard-code the selector in the JS code (not outputted by PHP), everything works OK and no error returned in the console. This is the code where I hard-coded the element selector:

奇怪的是,如果我在 JS 代码中硬编码选择器(不是由 PHP 输出),一切正常,控制台中没有返回错误。这是我硬编码元素选择器的代码:

var element_selector='.idoc-featured-slider';

Is there a way to use PHP to output the selector and not encountering the error? Thanks for any help.

有没有办法使用PHP输出选择器而不遇到错误?谢谢你的帮助。

回答by Leif Nelson

I ran into a similar issue. I was getting the following error:

我遇到了类似的问题。我收到以下错误:

Uncaught TypeError: Cannot read property 'nodeType' of undefined

未捕获的类型错误:无法读取未定义的属性“nodeType”

With these dialog position configuration values:

使用这些对话框位置配置值:

position: {my: "center", at: "left top", of: "window"}

Per the jQuery UI documentation, the value of the "of" property is an object rather than a string. So, when I changed the position values to the following:

根据 jQuery UI 文档,“of”属性的值是一个对象而不是字符串。因此,当我将位置值更改为以下内容时:

position: {my: "center", at: "left top", of: window}

the error disappeared.

错误消失了。

回答by user2120978

The problem is due to the of:$()not working if the object/selector is not valid.

问题是由于of:$()对象/选择器无效时无法工作。

回答by Manan Patel

Normally this Kind of error happen while you don't apply Properties of methods like prop() E.g:$(#"id").prop().if you don't specified its properties than also this error occur.

通常,当您不应用 prop() 等方法$(#"id").prop()的属性时会发生这种错误,例如:如果您没有指定其属性,也会发生此错误。

回答by Brijesh Ray

In my case i just changed the following in my and the error [DataTables warning: table id=bootstrap-data-table2 - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4]was gone:

在我的情况下,我只是在我的和错误[DataTables 警告:table id=bootstrap-data-table2 - Requested unknown parameter '0' for row 0 中更改了以下内容。 有关此错误的更多信息,请参阅http://datatables .net/tn/4]不见了:

From:

从:

                                    [enter image description here][1] <tbody>
                                            @foreach (var item in Model.HRMS_Tbl_ExpenseClaimModelList)
                                            {

                                                <tr>
                                                    @if (item.Approved == "1")
                                                    {
                                                        <td>@item.Date</td>
                                                        <td>@item.Date</td>
                                                        <td>@item.Type</td>
                                                        <td>@item.Amount</td>
                                                                               }
                                                </tr>

                                            }
                                        </tbody>


                        TO:


                                         <tbody>
                                            @foreach (var item in Model.HRMS_Tbl_ExpenseClaimModelList)
                                            {
                                              if (item.Approved == "1")
                                                    {
                                                <tr>

                                                        <td>@item.Date</td>
                                                        <td>@item.Date</td>
                                                        <td>@item.Type</td>
                                                        <td>@item.Amount</td>

                                                </tr>
                                                    }
                                            }
                                        </tbody>                            

回答by RITZZYYY

Please check in your code if any tags is not closed or not. I tried the same and it worked correctly. Apparently , I had an extra tag

如果任何标签未关闭或未关闭,请检查您的代码。我尝试了相同的方法并且它工作正常。显然,我有一个额外的标签