作者gaekeamql (芋頭)
看板Ajax
標題[心得] id重複的問題...
時間Thu Jul 21 15:29:41 2011
正常來講網頁id是不應該重複的
但是不小心還是會給他用到
如果這個時候 我有試 opera chrome ie8
例如:
<div class=aaa id=r1>123</div>
<div class=bbb id=r1>456</div>
此時 opera & chrome
jquery
$('.aaa#r1').html();
$('.bbb#r1').html();
此時 ie8 是不認識 第二個
都可以用的用法可以寫
$('.aaa[id=r1]').html();
$('.bbb[id=r1]').html();
不知道該說IE遵守標準還是.......
一定要把id搞一樣可以做
<div id=r1>
<div class=aaa>123</div>
<div class=bbb>456</div>
</div>
jquery
$('#r1').find('.aaa').html();
$('#r1').find('.bbb').html();
這樣是比較正統的寫法
有錯請多指教,我還很嫩!!..謝謝
--
█ █ █ http://www.u-tao.tw
█ █ ███ ▄▄█▄ ▄ ▄
█ █ █ █ ▉ █ ▉
█ █ █ █ ▉ █ ▉
◥▇▇▇◤ █ ◢ █ ▋▊ █ ▉
██ ▊█ ◥ ▍█
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.250.11
→ st900278:id 在一份html裡面 只能出現一次喔 07/21 15:57
→ st900278:就像是不會有兩個人有同個身分證一樣 id 不能重複 07/21 15:58
推 senser:id就是設計成要unique的 為什麼要這樣用哩? 07/21 17:33
→ mesak:最後一個可以這樣寫 $('#r1 > .aaa').html() 07/21 23:41
→ gaekeamql:喔!...有在jquery 有看到..剛剛才發現...=.=.感謝分享. 07/21 23:45
→ gaekeamql:會這樣寫id實在都是不得已的!. .=.=. 07/21 23:47
推 senser:>是CSS常用的選擇符 jquery也可以用.children([selector]) 07/24 06:46