如何多次實(shí)現(xiàn)點(diǎn)擊聚焦并傳遞參數(shù)?
<!DOCTYPE html>
<html>
<head>
? ? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
? ? <title></title>
? ? <style>
? ? .left div,
? ? .right div {
? ? ? ? width: 500px;
? ? ? ? height: 50px;
? ? ? ? padding: 5px;
? ? ? ? margin: 5px;
? ? ? ? float: left;
? ? ? ? border: 1px solid #ccc;
? ? }
? ? .left div {
? ? ? ? background: #bbffaa;
? ? }
? ??
? ? .right div {
? ? ? ? background: yellow;
? ? }
? ? </style>
? ? <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
? ? <h2>.focusin()方法</h2>
? ? <div class="left">
? ? ? ? <div class="aaron">
? ? ? ? ? ? 點(diǎn)擊聚焦:<input type="text" />
? ? ? ? ? ? <br>
? ? ? ? ? ? 點(diǎn)擊聚焦:<input type="text" />
? ? ? ? </div>
? ? </div>
? ? <div class="right">
? ? ? ? <div class="aaron1">
? ? ? ? ? ? 點(diǎn)擊聚焦并傳遞參數(shù):<input type="text" />
? ? ? ? ? ? <br>
? ? ? ? ? ? 點(diǎn)擊聚焦并傳遞參數(shù):<input type="text" />
? ? ? ? </div>
? ? ? ??
? ? </div>
? ? <script type="text/javascript">
? ? ? ? //input聚焦
? ? ? ? //給input元素增加一個(gè)邊框
? ? ? ? $("input:last-child").focusin(function() {
? ? ? ? ? ? ?$(this).css('border','12px solid red')
? ? ? ? })
? ? ? ??
? ? ? ? //D
? ? ? ? $("input:first").focusin(function(){
? ? ? ? ? ? $(this).css('border',"12px solid green")
? ? ? ? })
? ? </script>
? ? <script type="text/javascript">
? ? ? ? //不同函數(shù)傳遞數(shù)據(jù)
? ? ? ? function fn(e) {
? ? ? ? ? ? ?$(this).val(e.data)
? ? ? ? }
? ? ? ? function a() {
? ? ? ? ? ? $('input:first-child.focusin('慕課網(wǎng)', fn)
? ? ? ? }
? ? ? ? a();
? ??
? ? ? ? //DIY
? ? ? ? function fn(e){
? ? ? ? ? ? $(this).val(e.data)
? ? ? ? }
? ? ? ??
? ? ? ? function a(){
? ? ? ? ? ? $("input:last-child").focusin("請(qǐng)輸入密碼",fn)
? ? ? ? }
? ? ? ? a();
? ? </script>
</body>
</html>
黑體是我自己增加的,為什么這時(shí)候第一個(gè)框就無(wú)法實(shí)現(xiàn)點(diǎn)擊聚焦并傳遞參數(shù)了?
2016-10-12
<!DOCTYPE html>
<html>
<head>
? ? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
? ? <title></title>
? ? <style>
? ? .left div,
? ? .right div {
? ? ? ? width: 500px;
? ? ? ? height: 50px;
? ? ? ? padding: 5px;
? ? ? ? margin: 5px;
? ? ? ? float: left;
? ? ? ? border: 1px solid #ccc;
? ? }
? ? .left div {
? ? ? ? background: #bbffaa;
? ? }
? ??
? ? .right div {
? ? ? ? background: yellow;
? ? }
? ? </style>
? ? <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
? ? <h2>.focusin()方法</h2>
? ? <div class="left">
? ? ? ? <div class="aaron">
? ? ? ? ? ? 點(diǎn)擊聚焦:<input type="text" />
? ? ? ? ? ? <br>
? ? ? ? ? ? 點(diǎn)擊聚焦:<input type="text" />
? ? ? ? </div>
? ? </div>
? ? <div class="right">
? ? ? ? <div class="aaron1">
? ? ? ? ? ? 點(diǎn)擊聚焦并傳遞參數(shù):<input type="text" />
? ? ? ? ? ? <br>
? ? ? ? ? ? 點(diǎn)擊聚焦并傳遞參數(shù):<input type="text" />
? ? ? ? </div>
? ? ? ??
? ? </div>
? ? <script type="text/javascript">
? ? ? ? //input聚焦
? ? ? ? //給input元素增加一個(gè)邊框
? ? ? ? $("input:eq(1)").focusin(function() {
? ? ? ? ? ? ?$(this).css('border','1px solid red')
? ? ? ? })
? ? ? ??
? ? ? ? //D
? ? ? ? $("input:eq(0)").focusin(function(){
? ? ? ? ? ? $(this).css('border',"1px solid green")
? ? ? ? })
? ? </script>
? ? <script type="text/javascript">
? ? ? ? //不同函數(shù)傳遞數(shù)據(jù)
? ? ? ? function fn(e) {
? ? ? ? ? ? ?$(this).val(e.data)
? ? ? ? }
? ? ? ? function b() {
? ? ? ? ? ? $("input:eq(2)").focusin("慕課網(wǎng)", fn)
? ? ? ? }
? ? ? ? b();
? ??
? ? ? ? //DIY
? ? ? ? function fn(e){
? ? ? ? ? ? $(this).val(e.data)
? ? ? ? }
? ? ? ??
? ? ? ? function a(){
? ? ? ? ? ? $("input:eq(3)").focusin("請(qǐng)輸入密碼",fn)
? ? ? ? }
? ? ? ? a();
? ? </script>
</body>
</html>
2016-10-13
自己對(duì)比了下兩段代碼發(fā)現(xiàn)如下
1.首先選擇器last和last child,first和first child是不一樣的
????四個(gè)input分別為eq(0)-(3)
????在點(diǎn)擊聚焦和點(diǎn)擊聚焦并傳遞參數(shù)的2段代碼中,
????如果用
?????$("input:last")作為選擇器,結(jié)果只改變了eq(3)
?????$("input:last-child")作為選擇器,改變eq(1)和eq(3)
?????$("input:first")作為選擇器,結(jié)果只改變了eq(0)
?????$("input:first-child")作為選擇器,改變eq(0)和eq(2)
?????多個(gè)“child"就需要滿足兩個(gè)條件:
???? 被選取input有父元素,而且input是該父元素的第一個(gè)元素
2.//不同函數(shù)傳遞數(shù)據(jù)中兩個(gè)函數(shù)起名不能重復(fù).
3. ?$('input:first-child.focusin('慕課網(wǎng)', fn) 中漏了一個(gè))?
還要再打牢基礎(chǔ).