var obj = new Object();//1.原始----
obj.name ='php';
obj.rand=function(){
console.log(Math.random());
}
obj.sum=fucntion(num1,num2){
return num1+num2;
}
console.log(obj.sum(1+9));
//以空{}的形式创建对象,属性、方法--------------
var obj ={}; //2.进阶--------
obj.name="javascript";
console.log(obj.name);
obj.rand=function(){
console.log(Math.random());
}
obj.rand();
//以字典的形式创建对象,属性、方法-------------
var obj1={ //3.更加高级
name:'php',
rand:function(){
console.log(Math.random());
},
sum:function(num1,num2){
return num1+num2;
}
}
console.log(obj1.sum(11,22));
//常用对象创建方法
var obj ={
name:'test',
find:function(){
return this.name;
},
where:function(){
return this;
}
}
var obj1=obj;//内存地址同一个指向
obj1.name='test222';//这是指向同一个内存地址,所以只要属性一改,就会把前面的属性给改变
console.log(obj1.find());//test222
console.log(obj.find());//test222
//---------------------------------------------jquery学习-------
<script type="text/javascript" src="xxx.jquery.js"> </script>引入jquery
jquery模拟
function $(selector){//jquery的$()定义,可以理解为定义了一个函数
var _selector=selector.substr(0,1);
if(_selector == '.'){
return document.getElementsByClassName(selector.substr(1,selector.length-1));
}
if(_selector == '#'){
return document.getElementById(selector.substr(1,selector.length-1));
}
}
$res=$('.xxx');
$res=$('#xxx');
//jquery 实例应用-------------------------------------------------------------------
<html>
<head>
<script type="text/javascript" src="jquery-3.4.1.min.js"> </script>
</head>
<body>
<div id="for_id" style="background-color:green;width:200px;height:200px" onmouseover="overa()" onmouseleave="leavea()">
111</div>
<p class="for_class" style="background-color:yellow;width:300px;height:300px"></p>
<button onclick="overa()"> 点击改变</botton>
</body>
</html>
<script type="text/javascript" >
function overa(){
// console.log($('#for_id'));
// $('#for_id').css('background-color,red');
//var res=$('#for_id');
//console.log("res");
res.css("width","300px");//以字符串的形式,一属性,一值
res.css("height","300px");
res.css("background","red");
}
function leavea(){
// console.log($('#for_id'));
// $('#for_id').css('background-color,red');
//var res=$('#for_id');
// console.log("res");
$('#for_id').css({background:"green",width:"500px",height:"20px"});//以对象{}的形式,可以设置多个属性和值
}
</script>