我如何提取从HTML中收集的名字吗? [英] How do I extract the name from HTML collection?

查看:174
本文介绍了我如何提取从HTML中收集的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是通过javascript的步骤。

在这里输入的形象描述

我的HTMLCollection,我想通过它循环和FileUploadControl的ID中提取的对象。我该怎么做呢?

下面是我的code,我该如何进行呢?

 函数uploadImage(LNK)
{
    VAR排= lnk.parentNode.parentNode;
    VAR的rowIndex = row.rowIndex - 1;
    VAR ABC = row.cells [2] .getElementsByTagName(输入);
    VAR ARR = [] .slice.call(ABC);
}


解决方案

这将做到这一点:

  abc.namedItem('FileUploadControl')

但要注意的是:


  

不同的浏览器的行为不同,当有一个以上的
  匹配字符串元素用作索引(或namedItem的
  论据)。火狐8表现为在DOM 2 DOM4规定,返回
  第一匹配部件。 WebKit浏览器和Internet Explorer中
  这种情况下返回另一个的HTMLCollection和Opera返回的NodeList
  所有匹配的元素。


这里。

这意味着,如果你的标记你有这样的事情:

 < D​​IV ID ='ID'>富< / DIV>
< D​​IV ID ='ID'>酒吧和LT; / DIV>

执行以下code时,浏览器会表现不同:

  document.getElementsByTagName('DIV')。namedItem(ID)`

火狐将返回一个 HTML元素,而IE将返回另外的HTMLCollection。

要解决这个矛盾,您可以应用一个函数返回相同的对象。

  retElement(abc.namedItem('身份证'));

这可能是这样的:

  VAR retElement =功能(oElem){
  //火狐
  如果(oElem的instanceof window.HTMLElement)
    返回oElem;
  // IE和WebKit
  如果(oElem的instanceof window.HTMLCollection)
    返回oElem.item(0);
};

Here's the step through of the javascript.

I have an HTMLCollection, I want to loop through it and extract the object with the ID of FileUploadControl. How do I do it?

Here's my code, how do I proceed?

    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);
}

解决方案

This would do it:

abc.namedItem('FileUploadControl') 

But beware that:

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.

From here.

This means that if in your markup you had something like this:

<div id='id'>Foo</div>
<div id='id'>Bar</div>

browsers would behave differently when executing the following code:

document.getElementsByTagName('div').namedItem('id')`

Firefox would return an HTMLElement, while IE would return another HTMLCollection.

To solve this inconsistencies, you could apply a function to return the same object.

retElement( abc.namedItem('id') );

Which could be something like this:

var retElement = function(oElem) {
  //Firefox
  if (oElem instanceof window.HTMLElement)
    return oElem;
  //IE and WebKit
  if (oElem instanceof window.HTMLCollection)
    return oElem.item(0);
};

这篇关于我如何提取从HTML中收集的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆