Re: [討論] [jQuery] 相同 class 的按鈕對應到個別 …
基本上我還是覺得給id比較好用。
下面是如果是我的話,我會這樣寫,看起來會像是個控制中心。
可以把alert換成各個你自己寫的function或是直接寫你要的code。
希望能有人來打擊一下我的寫法,我才開始碰程式不到一年,需要人糾正。
<div class="button" id="btn1">button 1</div>
<div class="button" id="btn2">button 2</div>
<div class="button" id="btn3">button 3</div>
<div class="button" id="btn4">button 4</div>
<div class="button" id="btn5">button 5</div>
<script type="text/javascript">
function switchCenter(e){
switch(this.id){
case 'btn1':
alert(1);
break;
case 'brn2':
alert(2);
break;
default:
alert('others');
}
}
$('.button').click(switchCenter);
</script>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.84.254.88
→
03/18 01:37, , 1F
03/18 01:37, 1F
→
03/18 08:38, , 2F
03/18 08:38, 2F
→
03/18 08:39, , 3F
03/18 08:39, 3F
→
03/18 08:44, , 4F
03/18 08:44, 4F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 2 之 3 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章