js面向过程与面向对象编程示例(使用jquery库)

<!DOCTYPE html>
<html>
	<head>
		<style type="text/CSS">
			.firstDiv{ float:left; background:pink;}
			.firstDiv:hover{ cursor:pointer;}
			.noSelected{ -moz-user-select:none; -webkit-user-select:none; -ms-user-select:none; user-select:none;}
			.secondDiv{ background:gray; width:200px; height:400px; float:left; display:none; padding-top:30px; padding-left:30px;}
		</style>
	</head>
	<body>
		<div id="firstDiv" class="firstDiv"><span class="noSelected">鼠标滑动查看信息。</span></div>
		<div id="secondDiv" class="secondDiv">
			<p>qq:1900098</p>
			<p>tel:99998888</p>
			<p>user:xq</p>
		</div>
		<script type="text/JavaScript" src="../publicJQuery/jquery-2.1.4.js"></script>
		<script type="text/JavaScript">
		
			/*面向过程编程,首先得到某某,然后执行某某操作,叙事般往下走
			$("#firstDiv").hover(function(){$("#secondDiv").css("display","block");} , function(){$("#secondDiv").css("display","none");});
			$("#secondDiv").hover(function(){$("#secondDiv").css("display","block");} , function(){$("#secondDiv").css("display","none");});
			*/
			/*创建一个showAndHideDiv对象,属性为传入的参数,方法为show()跟hide()方法*/
			function showAndHideDiv(element){
				/* 不明显的面向对象
				this.show=function(){
					$(element).css("display","block");
				}
				
				this.hide=function(){
					$(element).css("display","none");
				}
				*/
				this.element=element;
			}
			
			showAndHideDiv.prototype={
				show:function(){
					$(this.element).css("display","block");
				},
				hide:function(){
					$(this.element).css("display","none");
				}
			}
			var divForChange=new showAndHideDiv("#secondDiv");
			$("#firstDiv,#secondDiv").hover(function(){divForChange.show();} ,function(){divForChange.hide();});
			//$("#secondDiv").hover( function(){divForChange.show();}, function(){divForChange.hide();});
				
		</script>
	</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值