如何从 HTML 集合中提取名称?
How do I extract the name from HTML collection?
这是 javascript 的步骤。
我有一个 HTMLCollection,我想遍历它并提取 ID 为 FileUploadControl 的对象。我该怎么做?
这是我的代码,我该如何继续?
function uploadImage(lnk)
{
var row = lnk.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
var abc = row.cells[2].getElementsByTagName("input");
var arr = [].slice.call(abc);
}
这样就可以了:
abc.namedItem('FileUploadControl')
但要注意:
Different browsers behave differently when there are more than one
elements matching the string used as an index (or namedItem's
argument). Firefox 8 behaves as specified in DOM 2 and DOM4, returning
the first matching element. WebKit browsers and Internet Explorer in
this case return another HTMLCollection and Opera returns a NodeList
of all matching elements.
来自 here.
这意味着如果在您的标记中有这样的内容:
<div id='id'>Foo</div>
<div id='id'>Bar</div>
浏览器在执行以下代码时会有不同的行为:
document.getElementsByTagName('div').namedItem('id')`
Firefox 会 return 一个 HTMLElement,而 IE 会 return 另一个 HTMLCollection。
要解决此不一致问题,您可以将函数应用于 return 同一对象。
retElement( abc.namedItem('id') );
可能是这样的:
var retElement = function(oElem) {
//Firefox
if (oElem instanceof window.HTMLElement)
return oElem;
//IE and WebKit
if (oElem instanceof window.HTMLCollection)
return oElem.item(0);
};
这是 javascript 的步骤。
我有一个 HTMLCollection,我想遍历它并提取 ID 为 FileUploadControl 的对象。我该怎么做?
这是我的代码,我该如何继续?
function uploadImage(lnk)
{
var row = lnk.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
var abc = row.cells[2].getElementsByTagName("input");
var arr = [].slice.call(abc);
}
这样就可以了:
abc.namedItem('FileUploadControl')
但要注意:
Different browsers behave differently when there are more than one elements matching the string used as an index (or namedItem's argument). Firefox 8 behaves as specified in DOM 2 and DOM4, returning the first matching element. WebKit browsers and Internet Explorer in this case return another HTMLCollection and Opera returns a NodeList of all matching elements.
来自 here.
这意味着如果在您的标记中有这样的内容:
<div id='id'>Foo</div>
<div id='id'>Bar</div>
浏览器在执行以下代码时会有不同的行为:
document.getElementsByTagName('div').namedItem('id')`
Firefox 会 return 一个 HTMLElement,而 IE 会 return 另一个 HTMLCollection。
要解决此不一致问题,您可以将函数应用于 return 同一对象。
retElement( abc.namedItem('id') );
可能是这样的:
var retElement = function(oElem) {
//Firefox
if (oElem instanceof window.HTMLElement)
return oElem;
//IE and WebKit
if (oElem instanceof window.HTMLCollection)
return oElem.item(0);
};