回答編集履歴
1
別の実現方法、プラグイン版を追記
test
CHANGED
@@ -199,3 +199,173 @@
|
|
199
199
|
</html>
|
200
200
|
|
201
201
|
```
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
---
|
206
|
+
|
207
|
+
追記:プラグイン版
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
自分だと、これくらいの規模でもプラグイン化してしまうことが多いです。コードがまとまって見やすいから、というのと、多分に趣味的な問題で。
|
212
|
+
|
213
|
+
ここでは、できるだけ最初に必要な情報を求めておき、ボタンを押したときの動作が速くなるようにしてみました。まあ、ボタン押してから id で要素を引いてきて `.text()` で中身を取ってきてもいっこうに構わないレスポンスタイムは出せると思いますけど。
|
214
|
+
|
215
|
+
```html
|
216
|
+
|
217
|
+
<!DOCTYPE html>
|
218
|
+
|
219
|
+
<html>
|
220
|
+
|
221
|
+
<head>
|
222
|
+
|
223
|
+
<meta charset="UTF-8">
|
224
|
+
|
225
|
+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
|
226
|
+
|
227
|
+
<style>
|
228
|
+
|
229
|
+
.contentArea {
|
230
|
+
|
231
|
+
display: none;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
.buttonArea ul {
|
238
|
+
|
239
|
+
display: table;
|
240
|
+
|
241
|
+
background-color: pink;
|
242
|
+
|
243
|
+
border-radius: 10%;
|
244
|
+
|
245
|
+
padding: 2px;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
.buttonArea ul li.button {
|
250
|
+
|
251
|
+
display: table-cell;
|
252
|
+
|
253
|
+
border-left: 1px solid white;
|
254
|
+
|
255
|
+
padding: 2px;
|
256
|
+
|
257
|
+
margin: 2px;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
.buttonArea ul li.button:first-child {
|
262
|
+
|
263
|
+
border-left: none;
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
.displayArea {
|
270
|
+
|
271
|
+
width: 500px;
|
272
|
+
|
273
|
+
border: 2px solid gray;
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
.displayArea .box {
|
278
|
+
|
279
|
+
display: table;
|
280
|
+
|
281
|
+
border: 2px solid slateblue;
|
282
|
+
|
283
|
+
margin: 1px;
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
</style>
|
288
|
+
|
289
|
+
</head>
|
290
|
+
|
291
|
+
<body>
|
292
|
+
|
293
|
+
<div class="contentArea">
|
294
|
+
|
295
|
+
<ul>
|
296
|
+
|
297
|
+
<li id="content01">a1</li>
|
298
|
+
|
299
|
+
<li id="content02">a2</li>
|
300
|
+
|
301
|
+
<li id="content03">a3</li>
|
302
|
+
|
303
|
+
<li id="content04">a4</li>
|
304
|
+
|
305
|
+
<li id="content05">a5</li>
|
306
|
+
|
307
|
+
<li id="content06">a6</li>
|
308
|
+
|
309
|
+
</ul>
|
310
|
+
|
311
|
+
</div>
|
312
|
+
|
313
|
+
<div class="buttonArea"></div>
|
314
|
+
|
315
|
+
<div class="displayArea"></div>
|
316
|
+
|
317
|
+
<script>
|
318
|
+
|
319
|
+
$.fn.mybox = function(opts) {
|
320
|
+
|
321
|
+
var $mybox = $(this);
|
322
|
+
|
323
|
+
opts = $.extend({ buttons: [] }, opts );
|
324
|
+
|
325
|
+
var $buttonContainer = $('<ul>').appendTo($mybox.find('.buttonArea'));
|
326
|
+
|
327
|
+
var $displayArea = $mybox.find('.displayArea');
|
328
|
+
|
329
|
+
var buttons = $mybox.find('.contentArea li').map(function() {
|
330
|
+
|
331
|
+
var $content = $(this);
|
332
|
+
|
333
|
+
var content = $content.text();
|
334
|
+
|
335
|
+
var seq = $content.attr('id').substr(7);
|
336
|
+
|
337
|
+
var $button = $('<li class="button">b' + seq + '</li>')
|
338
|
+
|
339
|
+
.appendTo($buttonContainer)
|
340
|
+
|
341
|
+
.on('click', function() {
|
342
|
+
|
343
|
+
$displayArea.append($('<div class="box">').append(content));
|
344
|
+
|
345
|
+
return false;
|
346
|
+
|
347
|
+
})
|
348
|
+
|
349
|
+
;
|
350
|
+
|
351
|
+
if (opts.buttons.indexOf(seq) < 0) $button.hide();
|
352
|
+
|
353
|
+
return $button;
|
354
|
+
|
355
|
+
});
|
356
|
+
|
357
|
+
};
|
358
|
+
|
359
|
+
$(function() {
|
360
|
+
|
361
|
+
$('body').mybox( { buttons: ['02', '03', '05'] } );
|
362
|
+
|
363
|
+
});
|
364
|
+
|
365
|
+
</script>
|
366
|
+
|
367
|
+
</body>
|
368
|
+
|
369
|
+
</html>
|
370
|
+
|
371
|
+
```
|