Blogger Information
Blog 24
fans 2
comment 5
visits 19005
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js创建对象,属性和方法,jQuery选择器、each函数练习--2019-10-23
木槿昔年的博客
Original
703 people have browsed it

js创建对象,属性和方法,jQuery选择器、each函数练习

js创建对象用关键字new Object()来表示,也可以用var obj = {} 来表示定义一个空对象,对象里有属性和方法

js创建对象,属性和方法

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>js对象的练习</title>
	<script>
	//定义js对象方法一
	var obj = new Object();
	obj.name = '我是js的一个属性值';
	obj.func = function (){
		return '我是js对象的一个方法';
	}

	//打印对象
	console.log(obj.name);
	console.log(obj.func());

	//定义js对象方法二
	var obj2 = {};

	obj2.name = '西门老师';
	obj2.project ='PHP中文网讲师';
	obj2.func = function(){
		return this.name+'是'+this.project;
	}
	console.log(obj2.func());

	//定义js对象方法三
	var obj3 = {
		name:'西门老师',
		project :'PHP中文网讲师',
		func:function(){
			return this.name+'是'+this.project;
		}

	}

	console.log(obj3.func());
	</script>
</head>
<body>
	<div id="teacher">
		<p class="info">PHP中文网朱老师</p>
		<p class="info">PHP中文网欧阳老师</p>
		<p class="info">PHP中文网西门老师</p>
	</div>
</body>
</html>

运行实例 »

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

jQuery选择器、each函数练习

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>jquery选择器和each()函数</title>
	<script src='https://www.php.cn/static/blogs/js/jquery3.4.1.js'></script>
	<script>
	$(function(){
		$('#teacher').css('background','#666');
		$('.info').css('color','#f60');

		$('#teacher p').each(function(){
			alert($(this).text())
		});

	})
	</script>
</head>
<body>
	<div id="teacher">
		<p class="info">PHP中文网朱老师</p>
		<p class="info">PHP中文网欧阳老师</p>
		<p class="info">PHP中文网西门老师</p>
	</div>
</body>
</html>

运行实例 »

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

以前没有接触过js对象,通过老师的讲解对js有了更清晰的认识。

Correction status:qualified

Teacher's comments:each()与原生的forEach()和map()功能类似
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