简体   繁体   English

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

[英]Uncaught TypeError: Cannot read properties of undefined (reading 'indexOf')

I'm selecting all child divs of area_enInfantry and looping through to adjust the text of certain ones.我正在选择 area_enInfantry 的所有子 div 并循环调整某些子 div 的文本。 cardArray is a global const and myId is defined within the parent function. cardArray 是一个全局常量,myId 在父 function 中定义。

Uncaught TypeError: Cannot read properties of undefined (reading 'getAttribute')未捕获的类型错误:无法读取未定义的属性(读取“getAttribute”)

 var field = $("#area_en" + cardArray[myId]['type']).find("div"); field.each(function(a, element) { console.log("cC type:" + cardArray[myId]['type'] + "- index:" + a + " title: " + element[a].attr('title')); if (element[a].attr("title").indexOf("player") < 0) { // check this card isn't special player card doStuff; } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="area_enInfantry"> <div id="info_enInfantryScore" class="info_Score">-1</div> <div class="encardContainer" title="barricade">-1</div> <div class="encardContainer" title="spy">2</div> </div>

I read on this post that it may be because the contents of field/element may be DOM elements and that I should wrap them in $() so I did exactly that- changing both variables to $(element)[a].attr('title') but now I get Uncaught TypeError: Cannot read properties of undefined (reading 'indexOf') instead, seemingly moving the error to the next line.在这篇文章中读到,这可能是因为字段/元素的内容可能是 DOM 元素,我应该将它们包装在 $() 中,所以我确实这样做了 - 将两个变量都更改为 $(element)[a].attr( 'title') 但现在我得到 Uncaught TypeError: Cannot read properties of undefined (reading 'indexOf') ,似乎将错误移至下一行。

What am I doing wrong here?我在这里做错了什么?

There's a couple of issues here.这里有几个问题。 Firstly the variable element contains a Element object, not a jQuery object, so there is no attr() method available.首先,变量element包含元素 object,而不是 jQuery object,因此没有可用的attr()方法。

Secondly, once you correct that, attr('title') on the first div is not set so is undefined .其次,一旦你纠正了这一点,第一个div上的attr('title')就没有设置,所以undefined Therefore you'll get an error because you're calling indexOf() on an empty value.因此你会得到一个错误,因为你在一个空值上调用indexOf() You can use solve this by coalescing the value to an empty string if null.如果 null,您可以通过将值合并为空字符串来解决此问题。

Also note that I would assume you want to invoke the doStuff() function, so need to add the () at the end and it's better practice to use prop() over attr() where possible.另请注意,我假设您要调用doStuff() function,因此需要在末尾添加() ,最好尽可能使用prop()而不是attr()

With that said, try this:话虽如此,试试这个:

 // mock data let myId = 'foo'; let cardArray = { foo: { type: 'Infantry' } } let doStuff = () => console.log('do stuff...'); var $field = $("#area_en" + cardArray[myId]['type']).find("div"); $field.each(function(i, element) { let $el = $(element); console.log("cC type:" + cardArray[myId]['type'] + "- index:" + i + " title: " + $el.prop('title')); if (($el.prop("title") || '').indexOf("player") < 0) { // check this card isn't special player card doStuff(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="area_enInfantry"> <div id="info_enInfantryScore" class="info_Score">-1</div> <div class="encardContainer" title="barricade">-1</div> <div class="encardContainer" title="spy">2</div> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性(读取&#39;indexOf&#39;) - TypeError: Cannot read properties of undefined (reading 'indexOf') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') ReactJS/Firebase:TypeError:无法读取未定义的属性(读取“indexOf”) - ReactJS/Firebase: TypeError: Cannot read properties of undefined (reading 'indexOf') 错误:TypeError:无法读取未定义的属性(读取“indexOf”) - ERROR: TypeError: Cannot read properties of undefined (reading 'indexOf') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') 未捕获的类型错误:无法读取未定义的属性“indexOf” - Uncaught TypeError: Cannot read property 'indexOf' of undefined 未捕获的类型错误:无法读取未定义的属性“indexOf” - Uncaught TypeError: Cannot read property 'indexOf' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM