javascript - trick IE form and its expando attribute

本文介绍了一个在IE浏览器中遇到的问题,即某些输入元素被误认为是表单对象的属性。通过使用getAttributeNode方法,可以正确地获取表单元素的实际属性。文章还提供了测试代码和示例来验证解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

there is a known issue that if you have a form with several inupt elements, and those elemnt happens to have specific id or names then the elements becomes the expando attribute of the form element. 

 

e.g.. 

 

 

<body>
<form id="form" action="/">
<input type="text" id="id"/>
<input type="text" name="action"/>
</form>
</body>

 

 in this example, if you do 

 

form.id, then it return the DOM element, and if you do form.action, it returns another input element. 

 

so to work around, this, there is a method which is called getAttributeNode. please see the following code which first check the attribute node then check to see if there there is attribute with that name (the expando is equivalent of getAttribute) 

 

 

/**************************************
*@Name: ieformexpando.js
*@Summary
*  due to some reason, the IE will treat some input element as attribute to the form object  if the nput elements happens to have a specific name or ID
*
*
* <form id="form" action="/">
*  <input type="text" id="id"/>
* <input type="text" name="action"/>
* </form>
***************************************/

function attr(elem, name) {
  if (elem.nodeName.toUpperCase() === "form" && elem.getAttributeNode(name)) {
    return elem.getAttributeNode(name).nodeValue;
  }
  else {
    return elem.getAttribute(name);
  }
}
 

 

 

and below is the code that test the code above.

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- ieformexpando.html -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="../unit.js"></script>
    <script type="text/javascript" src="ieformexpando.js"></script>
    <script type="text/javascript">
      window.onload = function () {
        test("test IE expando ", function () {
          var form = document.getElementById("form");
          // both are 'form' and '/' accordingly
//          alert(attr(form, "id"));
//          alert(attr(form, "action"));
          assert(attr(form, "id") === 'form', "attr return the attribute node rather that the attribute where is faked by the element");
          assert(attr(form, "action") === '/', "attr return the attribute node rather that the attribute where is faked by the element");
        });
      };
    </script>
    <style type="text/css">
      #results li.pass { color:Green }
      #results li.fail { color:red}
    </style>
</head>
<body>
<ul id="results" />
<form id="form" action="/">
<input type="text" id="id"/>
<input type="text" name="action"/>
</form>
</body>
</html>
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值