javascript学习指南_JavaScript中setAttribute用法详解

更新时间:2019-12-08    来源:网页配色    手机版     字体:

【www.bbyears.com--网页配色】

setAttribute基本用法

element.setAttribute(attributename,attributevalue)

setAttribute() 方法添加指定的属性,并为其赋指定的值,看到w3c的例子

document.getElementsByTagName("INPUT")[0].setAttribute("type","button");

我们经常需要在JavaScript中给Element动态添加各种属性,这可以通过使用setAttribute()来实现,这就涉及到了浏览器的兼容性问题。

var bar = document.getElementById("foo");
bar.setAttribute("onclick", "javascript:alert("This is a test!");");

这里利用setAttribute指定e的onclick属性,简单,很好理解。但是IE不支持,IE并不是不支持setAttribute这个函数,
而是不支持用setAttribute设置某些属性,例如对象属性、集合属性、事件属性,也就是说用setAttribute设置style和onclick这些属性
在IE中是行不通的。为达到兼容各种浏览器的效果,可以用点符号法来设置Element的对象属性、集合属性和事件属性。

程序代码

document.getElementById("foo").className = "fruit";
document.getElementById("foo").style.cssText = "color: #00f;";
document.getElementById("foo").style.color = "#00f";
document.getElementById("foo").onclick= function () { alert("This is a test!"); }

例子



   
       
        Untitled Document
        <script language="JavaScript">
            function change() {
                var input = document.getElementById("li1");
                alert(input.getAttribute("title"));
                input.setAttribute("title", "mgc");
                alert(input.getAttribute("title"));
            }
        </script>
   
   
       


               
  • Magci
                J2EE
                Haha!
           
           
       

    例子,一个企业网站用到的就是当我们点击时自动给href重新赋值然后点击有值了







    aaa
    aaabb
    <script language="javascript">

    function testaa()
    {
     var url = window.location.href;
     var array = url.split("#");
     if(array[1]==undefined)
     {
      array[1] ="home";
     }
     //Dpre 父级
     //Dnext 子级
     switch(array[1])
     {
      case "home":
       getid("Dpre").setAttribute("href","index.html#home");
       getid("Dnext").setAttribute("href","index.html#xmqw");
       
       break;
      case "xmqw":
       getid("Dpre").setAttribute("href","index.html#home");
       getid("Dnext").setAttribute("href","index.html#sspj");
       break;
      case "sspj":
       getid("Dpre").setAttribute("href","index.html#xmqw");
       getid("Dnext").setAttribute("href","index.html#cpjd");
       break;
      case "cpjd": 
       getid("Dpre").setAttribute("href","index.html#sspj");
       getid("Dnext").setAttribute("href","index.html#zyzx");
       break; 
      case "zyzx":
       getid("Dpre").setAttribute("href","index.html#cpjd");
       getid("Dnext").setAttribute("href","index.html#zxdt");
       break; 
      case "zxdt":
       getid("Dpre").setAttribute("href","index.html#zyzx");
       getid("Dnext").setAttribute("href","index.html#hrzy");
       break;
      case "hrzy":
       getid("Dpre").setAttribute("href","index.html#zxdt");
       getid("Dnext").setAttribute("href","index.html#home");
       break;
     }

    }

    function abc()
    {
     alert(0);
    }
    function getid(id)
    {
     return document.getElementById(id);
    }
    //alert(getid("xxx"));
    </script>


    好了到此关于小编对于js setAttribute就介绍到此了。

    本文来源:http://www.bbyears.com/wangyezhizuo/82655.html