SCRIPT5007 无法获取 属性 'indexOf' 的值
SCRIPT5007 Unable To get value of the property 'indexOf'
我需要帮助解决 IE9 中的这个错误:"SCRIPT5007 Unable To get value of the property 'indexOf': object is null or undefined"
findParent = function (father, str, prop) {
/**
* Go up the DOM tree and find the parent element with the specified class or prop name.
* @param {Object} father - (HTMLElement)
* @param {String} str - the class of the father
* @param {string} prop - some other property such as name or id
* @example var parentEl = findParent(container, 'genericForm', 'name');
*/
'use strict';
if (prop === undefined || typeof prop !== 'string') {
prop = 'className';
}
while ((father !== undefined || typeof father !== 'string' || father !== null) && (father = father.parentElement) && !(father[prop].indexOf(str) >= 0));
return father;
};
var container = document.getElementById('description');
var parentEl = findParent(container, 'gForm', 'name');
alert(parentEl);
<form action="/gform.html" class="campaign-forms" method="post" name="gForm">
<fieldset class="fieldset" title="Type your question...">
<textarea name="description" id="description" placeholder="Type your question..." data-ana-label="Type your question..."></textarea>
<small class="error"><i class="close">×</i> This is a required field</small>
</fieldset>
</form>
我希望在这种情况下 return <form>
。请帮忙。
看来,浏览器之间存在差异。当father[prop]
没有明确定义时,IE returns undefined
,其他浏览器好像return一个空字符串。
要解决此问题,您可以检测 undefined
并将其替换为空字符串,如下所示:
findParent = function (father, str, prop) {
'use strict';
if (prop === undefined || typeof prop !== 'string') {
prop = 'className';
}
while (father && (father = father.parentElement) && !((father[prop] || '').indexOf(str) >= 0));
^^^^^^^^^^^^^^^^^^^^
return father;
};
(我只是稍微简化了条件,如果你愿意,你可以使用原始的 father
-exists-detection。)
我需要帮助解决 IE9 中的这个错误:"SCRIPT5007 Unable To get value of the property 'indexOf': object is null or undefined"
findParent = function (father, str, prop) {
/**
* Go up the DOM tree and find the parent element with the specified class or prop name.
* @param {Object} father - (HTMLElement)
* @param {String} str - the class of the father
* @param {string} prop - some other property such as name or id
* @example var parentEl = findParent(container, 'genericForm', 'name');
*/
'use strict';
if (prop === undefined || typeof prop !== 'string') {
prop = 'className';
}
while ((father !== undefined || typeof father !== 'string' || father !== null) && (father = father.parentElement) && !(father[prop].indexOf(str) >= 0));
return father;
};
var container = document.getElementById('description');
var parentEl = findParent(container, 'gForm', 'name');
alert(parentEl);
<form action="/gform.html" class="campaign-forms" method="post" name="gForm">
<fieldset class="fieldset" title="Type your question...">
<textarea name="description" id="description" placeholder="Type your question..." data-ana-label="Type your question..."></textarea>
<small class="error"><i class="close">×</i> This is a required field</small>
</fieldset>
</form>
我希望在这种情况下 return <form>
。请帮忙。
看来,浏览器之间存在差异。当father[prop]
没有明确定义时,IE returns undefined
,其他浏览器好像return一个空字符串。
要解决此问题,您可以检测 undefined
并将其替换为空字符串,如下所示:
findParent = function (father, str, prop) {
'use strict';
if (prop === undefined || typeof prop !== 'string') {
prop = 'className';
}
while (father && (father = father.parentElement) && !((father[prop] || '').indexOf(str) >= 0));
^^^^^^^^^^^^^^^^^^^^
return father;
};
(我只是稍微简化了条件,如果你愿意,你可以使用原始的 father
-exists-detection。)