JQuery学习之旅(1)

98 阅读1分钟

JQuery的学习之旅(1)– 基础篇

**getElementById(id); 获取指定id的元素
object.style 是属性
通过对象object.style.property = xxx; 赋值,对对象的属性进行修改
object.className= xx; 改变对象的样式

通过object.reamoveAttribute(“style”) ,将对象的style重新设置。

**

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
    <title>document.getElementById</title>
    <style type="text/css">
        input{
            font-size:10px;
        }
        .one{
                border:1px solid #eee;
                width:230px;
                height:50px;
                background:#ccc;
                color:red;
        }.two{
                border:1px solid #ccc;
                width:230px;
                height:50px;
                background:#9CF;
                color:blue;
        }
    </style>
</head>
<body>
    <h2 id="con">JavaScript课程</H2>
    <div id="txt"> 
        <h5>JavaScript为网页添加动态效果并实现与用户交互的功能。</h5>
        <p>1. JavaScript入门篇,让不懂JS的你,快速了解JS。</p>
        <p>2. JavaScript进阶篇,让你掌握JS的基础语法、函数、数组、事件、内置对象、BOM浏览器、DOM操作。</p>
        <p>3. 学完以上两门基础课后,在深入学习JavaScript的变量作用域、事件、对象、运动、cookie、正则表达式、ajax等课程。</p>
   </div>

   <form>
        <input type="button" value="改变颜色" onclick="changeColor()"/>
        <input type="button" value="改变宽高" onclick="changeHeightAndWidth()"/>
        <input type="button" value="隐藏内容" onclick="hide()"/>
        <input type="button" value="显示内容" onclick="show()"/>
        <input type="button" value="取消设置" onclick="cancel()"/>
        <input type="button" value="添加样式" onclick="add()"/>
        <input type="button" value="改变样式" onclick="change()"/>
   </form>

   <script type="text/javascript">
        function changeColor() {
            var my = document.getElementById("con");
            my.style.color = "#fff";
            my.style.backgroundColor = "#CCC";
        }
        function changeHeightAndWidth() {
            var my = document.getElementById("con");
            my.style.height="100px";
            my.style.width="200px";
        }
        function show() {
            var my = document.getElementById("con");
            my.style.display="block";           
        }
        function hide() {
            var my = document.getElementById("con");
            my.style.display="none";            
        }
        function cancel() {

            var conf = confirm("消除设置吗?")    
            if(conf == true){
                var my = document.getElementById("con");
                //删除样式
                my.removeAttribute("style");
                my.className=null;
            } 
        }
        function add() {
            var my = document.getElementById("con");
            my.className = "one";
        }
        function change() {
            var my = document.getElementById("con");
            my.className = "two";
        }

   </script> 
</body>
</html>