自定义HTML属性,:Firefox与IE

Cha*_*adD 1 html javascript firefox internet-explorer

我需要对以下简单的HTML页面进行哪些更改才能让Firefox在IE中读取和设置自定义属性?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Original Value</title>
</head>

<body>

<div MyAttribute="Original Value"  id="Label1">Hello World</div>

<form method="post">
    <table style="width: 100%">
        <tr>
            <td>
            <input name="Button1" onclick="button1_click();" type="button" value="button" /></td>
        </tr>
    </table>
    <script type="text/javascript">
function button1_click(){

alert("Enter");
//alert("Label1.MyAttribute " + Label1.MyAttribute);
alert(Label1.getAttribute("MyAttribute"));

Label1.MyAttribute = "Updated";
alert("Label1.MyAttribute " + Label1.MyAttribute);

}
</script>
</form>

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Ant*_*nes 7

您已经发现getAttribute但需要检索元素,document.getElemenById并且需要使用它setAttribute来修改属性.

function button1_click()
{
  alert("Enter");
  var label1 = document.getElementById("Label1"); 
  alert(label1.getAttribute("MyAttribute"));
  label1.setAttribute("MyAttribute", "Updated");
  alert("Label1.MyAttribute " + label1.getAttribute("MyAttribute"));
}
Run Code Online (Sandbox Code Playgroud)