javascript 兼容性


老是写jQ的东西,不是很熟悉js原生态的操作,所以会开始慢慢在这里积累一些js的原生态操作的语法。

<!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>
</head>

<body>
<div class="test" id="test" mark="mark" name="test" style="width:400px; height:400px;">test1</div>
<div class="test1" id="test1" mark="mark" style="width:400px; height:400px;"></div>
<p style="color:red;width:400px; height:400px;">dddddddddddddddddddd</p>
<script type="text/javascript">
    //获取html元素
    var test = document.getElementById("test");
    //通过ID访问
    var test1 = document.getElementsByTagName("div")[0];
    //通过标签名访问
    var test2 = document.getElementsByName("");
    //通过name属性来访问
    
    //设置css
    test1.style.color = "red";
    test1.style.background = "blue";
    test.style.height = 500+"px";
    //设置属性
    test.getAttribute("mark");
    console.log(test.getAttribute("mark"));
    test.setAttribute("mark","999");
    console.log(test.getAttribute("mark"));
    
    //innerHTML 
    var test2 = document.getElementsByTagName("p")[0];
    console.log(test2.innerText);
    test2.innerHTML = "chaneg";
    //innerHTML
    
</script>
</body>
</html>


 

你可能感兴趣的:(JavaScript)