Blogger Information
Blog 34
fans 0
comment 0
visits 23017
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第14章 JavaScript 基础2-2019年10月23日20时00分
Tommy-黄天浩的博客
Original
617 people have browsed it

1、练习JS创建对象、属性、方法。

// 创建JavaScript对象
// 第一种方法
var obj=new Object();
obj.name='张三';
obj.sum=function (num1,num2) {
    return num1+num2;
};
var res=obj.sum(1,5);
console.log(res);
console.log(obj.name);
//第二种方法
var obj={
    name:'张三',
    sum:function (num1,num2) {
        return num1+num2;
    }
};
var res=obj.sum(1,5);
console.log(res);
console.log(obj.name);
//第三种方法
var obj={

};
obj.name='张三';
obj.sum=function (num1,num2) {
    return num1+num2;
};
var res=obj.sum(1,8);
console.log(res);
console.log(obj.name);

2、练习通过jquery的id、class选择器选择元素,$.each遍历数组

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="jquery-3.4.1.min.js"></script>
    <title>Document</title>
</head>
<body>
<div id="div1" style="background: aqua;width: 200px;height: 50px;"><h1 class="div2" style="color: brown">我是一段文字</h1></div>
<button onclick="change_id()">改变div背景颜色</button>
<button onclick="change_class()">改变文字颜色</button>

</body>
</html>
<script>
    function change_id(){
        $('#div1').css({'background':'yellow'});
    }
    function change_class() {
        $('.div2').css({'color':'blue'});
    }
</script>

遍历数组:

<script>
    var arr=[1,2,3,4,5,6,7,8,9];
    $.each(arr,function (k,v) {
        console.log('下标:'+k+'值:'+v);
    });
</script>


Correcting teacher:天蓬老师天蓬老师

Correction status:unqualified

Teacher's comments:继续努力
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post