一、操作元素属性
(1)元素属性操作
第一种:oDiv.style.display=‘block‘;
第二种:oDiv.style[‘display‘]=‘block‘;
第三种:Dom方式,为以下的3种方式
DOM方式操作元素属性:
获取:getAttribute(名称),在大多数情况下是用不着的。
设置:setAttribute(名称,值)
删除:removeAttribute(名称)
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>元素属性操作</title>
<script>
window.onload = function ()
{
var oTxt=document.getElementById(‘txt1‘);
var oBtn=document.getElementById(‘btn1‘);
oBtn.onclick = function ()
{
//oTxt.value=‘aaaaaaa‘;
//oTxt[‘value‘]=‘aaaaa‘;
oTxt.setAttribute(‘value‘,‘aaaaaaa‘);
};
};
</script>
</head>
<body>
<input id="txt1" type="text" />
<input id="btn1" type="button" value="按钮" />
</body>
</html>
原文:http://www.cnblogs.com/zhy2017/p/6359145.html