Blogger Information
Blog 23
fans 0
comment 0
visits 19754
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1、练习JS创建对象、属性、方法。通过jquery的id、class选择器选择元素,$.each遍历数组--1023
风吹的博客
Original
736 people have browsed it

JS创建对象的方法,总结写在代码注释里

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>js第三节</title>
</head>
<body>
	
</body>
</html>
<script type="text/javascript">
	//js创建对象的三种方法
	//1.
	var obj1 = new Object();
	//2.
	var obj2 = {};
	//3.
	var obj3 = {
		//写属性,方法

	};
	//第一种是通过new Object,第二种是直接创建一个空对象,第三种看起来要比前两种舒服一些,是在创建对象时就写入属性和方法。而前两种要在外部添加
	//具体写法如下
	//第一种
	var obj11 = new Object();
	obj11.text = '6666666666';
	obj11.sum = function(a,b){
		return a+b;
	},
	obj11.fun = function(){
		return '来了';
	}
	console.log(obj11.text);
	console.log(obj11.fun());
	console.log(obj11.sum(11,22));
	//第二种
	var obj22 = {};
	obj22.text = '1111'
	obj22.rand = function(){
		console.log(Math.random());
	}
    console.log(obj22.text);
	obj22.rand();
	//第三种
	var obj33 = {
		text:'99999',
		rand:function(){
			console.log(Math.random());
		}
	}
	console.log(obj33.text);
	obj33.rand();

</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例

1023....png


JS选择器与循环

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>js第三节</title>
	<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
</head>
<body>
	<p id="111">dsadsa</p>
	<p class="222">123sad</p>

</body>
</html>
<script type="text/javascript">
	$('#111').css('color','red');
	$('.222').css('color','blue');
	
	//jq循环遍历
	var arr = [1,456,45,12,313,123,];
	$.each(arr,function(i,v){
		console.log('键:'+i+'值:'+v);
	});


	
</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例

jsjs.png

$('#')对应html中id

$('.')对应html中的class

有几点要注意,在js中连接符是+号和php不同,php是.

关于循环$.each(要循环的数组,怎么循环) 


Correcting teacher:查无此人查无此人

Correction status:qualified

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!