回答編集履歴
7
少しコードを編集。何度もすみません。。(^ ^;
answer
CHANGED
@@ -41,7 +41,7 @@
|
|
41
41
|
<html>
|
42
42
|
<head>
|
43
43
|
<meta charset="utf-8">
|
44
|
-
<title>
|
44
|
+
<title>boxN</title>
|
45
45
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
|
46
46
|
<style>
|
47
47
|
.bx {
|
@@ -143,5 +143,6 @@
|
|
143
143
|
</body>
|
144
144
|
</html>
|
145
145
|
```
|
146
|
+
[jsfiddle](https://jsfiddle.net/namnium1125/4mctxLbw/)
|
146
147
|
|
147
148
|
これで互いに影響を及ぼすことは無いはずです。
|
6
少しコードwo編集
answer
CHANGED
File without changes
|
5
更に追記
answer
CHANGED
@@ -30,4 +30,118 @@
|
|
30
30
|
これで無制限に変更できます。
|
31
31
|
|
32
32
|
[jsfiddle](https://jsfiddle.net/namnium1125/bxkmhy8m/)
|
33
|
-
※このデモでは`7`までくると`1`に戻るようになっています。
|
33
|
+
※このデモでは`7`までくると`1`に戻るようになっています。
|
34
|
+
|
35
|
+
## 追記2
|
36
|
+
|
37
|
+
今までの話を踏まえて全体的に作り直してみました。
|
38
|
+
|
39
|
+
```javascript
|
40
|
+
<!doctype html>
|
41
|
+
<html>
|
42
|
+
<head>
|
43
|
+
<meta charset="utf-8">
|
44
|
+
<title>template</title>
|
45
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
|
46
|
+
<style>
|
47
|
+
.bx {
|
48
|
+
width: 100px;
|
49
|
+
height: 100px;
|
50
|
+
margin: 10px;
|
51
|
+
}
|
52
|
+
|
53
|
+
.wrapper{
|
54
|
+
display: -webkit-box;
|
55
|
+
display: -moz-box;
|
56
|
+
}
|
57
|
+
|
58
|
+
.box1{
|
59
|
+
background: red;
|
60
|
+
}
|
61
|
+
|
62
|
+
.box2{
|
63
|
+
background: orange;
|
64
|
+
}
|
65
|
+
|
66
|
+
.box3{
|
67
|
+
background: yellow;
|
68
|
+
}
|
69
|
+
|
70
|
+
.box4{
|
71
|
+
background: green;
|
72
|
+
}
|
73
|
+
|
74
|
+
.box5{
|
75
|
+
background: aqua;
|
76
|
+
}
|
77
|
+
|
78
|
+
.box6{
|
79
|
+
background: blue;
|
80
|
+
}
|
81
|
+
|
82
|
+
.box7{
|
83
|
+
background: purple;
|
84
|
+
}
|
85
|
+
</style>
|
86
|
+
<script>
|
87
|
+
$(function(){
|
88
|
+
// 初期化 全ての.bxにイベント付与
|
89
|
+
// click.chn -> changeBoxNumに関するclickイベントであることを明記 意味は無いかも。。(^ ^;
|
90
|
+
$('.bx').one('click.chn',function(e){
|
91
|
+
var clsNames = $(e.target).attr('class').split(' ');
|
92
|
+
var n = 1;
|
93
|
+
clsNames.forEach(function(val,ind,arr){
|
94
|
+
if(val.indexOf('box') != -1) n = parseInt(val.split('box')[1]);
|
95
|
+
});
|
96
|
+
changeBoxNum(n,e.target);
|
97
|
+
});
|
98
|
+
});
|
99
|
+
|
100
|
+
function changeBoxNum(n,et){
|
101
|
+
if(n<7){
|
102
|
+
$(et).removeClass('box'+n).addClass('box'+(n+1));
|
103
|
+
$(et).one('click.chn',function(e){
|
104
|
+
changeBoxNum(n+1,e.target);
|
105
|
+
});
|
106
|
+
}else{
|
107
|
+
$(et).removeClass('box7').addClass('box1');
|
108
|
+
$(et).one('click.chn',function(e){
|
109
|
+
changeBoxNum(1,e.target);
|
110
|
+
});
|
111
|
+
}
|
112
|
+
}
|
113
|
+
</script>
|
114
|
+
</head>
|
115
|
+
<body>
|
116
|
+
<div class="wrapper">
|
117
|
+
<div class="bx box1"></div>
|
118
|
+
<div class="bx box2"></div>
|
119
|
+
<div class="bx box3"></div>
|
120
|
+
<div class="bx box4"></div>
|
121
|
+
<div class="bx box5"></div>
|
122
|
+
<div class="bx box6"></div>
|
123
|
+
<div class="bx box7"></div>
|
124
|
+
</div>
|
125
|
+
<div class="wrapper">
|
126
|
+
<div class="bx box1"></div>
|
127
|
+
<div class="bx box2"></div>
|
128
|
+
<div class="bx box3"></div>
|
129
|
+
<div class="bx box4"></div>
|
130
|
+
<div class="bx box5"></div>
|
131
|
+
<div class="bx box6"></div>
|
132
|
+
<div class="bx box7"></div>
|
133
|
+
</div>
|
134
|
+
<div class="wrapper">
|
135
|
+
<div class="bx box1"></div>
|
136
|
+
<div class="bx box2"></div>
|
137
|
+
<div class="bx box3"></div>
|
138
|
+
<div class="bx box4"></div>
|
139
|
+
<div class="bx box5"></div>
|
140
|
+
<div class="bx box6"></div>
|
141
|
+
<div class="bx box7"></div>
|
142
|
+
</div>
|
143
|
+
</body>
|
144
|
+
</html>
|
145
|
+
```
|
146
|
+
|
147
|
+
これで互いに影響を及ぼすことは無いはずです。
|
4
コードの訂正
answer
CHANGED
@@ -20,6 +20,7 @@
|
|
20
20
|
});
|
21
21
|
|
22
22
|
function changeBoxNum(n){
|
23
|
+
$('.box'+n).off("click");
|
23
24
|
$('.box'+n).removeClass().addClass('box'+(n+1));
|
24
25
|
$('.box'+(n+1)).click(function(){
|
25
26
|
changeBoxNum(n+1);
|
3
デモの追加
answer
CHANGED
@@ -26,4 +26,7 @@
|
|
26
26
|
});
|
27
27
|
}
|
28
28
|
```
|
29
|
-
これで無制限に変更できます。
|
29
|
+
これで無制限に変更できます。
|
30
|
+
|
31
|
+
[jsfiddle](https://jsfiddle.net/namnium1125/bxkmhy8m/)
|
32
|
+
※このデモでは`7`までくると`1`に戻るようになっています。
|
2
追記しました
answer
CHANGED
@@ -9,4 +9,21 @@
|
|
9
9
|
});
|
10
10
|
```
|
11
11
|
これでとりあえず
|
12
|
-
`.box1`->`.box2`->`.box3`となります。
|
12
|
+
`.box1`->`.box2`->`.box3`となります。
|
13
|
+
|
14
|
+
## 追記
|
15
|
+
```javascript
|
16
|
+
$(function(){
|
17
|
+
$('.box1').click(function(){
|
18
|
+
changeBoxNum(1);
|
19
|
+
});
|
20
|
+
});
|
21
|
+
|
22
|
+
function changeBoxNum(n){
|
23
|
+
$('.box'+n).removeClass().addClass('box'+(n+1));
|
24
|
+
$('.box'+(n+1)).click(function(){
|
25
|
+
changeBoxNum(n+1);
|
26
|
+
});
|
27
|
+
}
|
28
|
+
```
|
29
|
+
これで無制限に変更できます。
|
1
コードの編集
answer
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
$('.box1').removeClass().addClass('box2');
|
5
5
|
$('.box2').on('click',function(){
|
6
6
|
$('.box2').removeClass().addClass('box3');
|
7
|
-
})
|
7
|
+
});
|
8
8
|
});
|
9
9
|
});
|
10
10
|
```
|