HTML5の新要素をjQueryでappendとかするとバグる件
- 2010年07月01日
- category:XHTML/CSS, javascript
- Comment(0)
- Trackback(0)
jQueryでsectionとかarticleとかをappendしたときにstyleが反映されなかったという話しを聞いたので調べた&検証してみたメモ。
調べてみたところ、定義されていない要素をinnerHTMLしてappendChildすると、以下の例の場合IE(6|7|8)では開始タグ、テキストノード、終了タグの3つの要素として追加されるとのこと。(ちなみにhtml5.jsは読んでます)
var div = document.createElement('div');
div.innerHTML = '<section>section</section>';
document.getElementById('box').appendChild(div);
以下のようにcreateElementすればいける。
var div = document.createElement('div');
var section = document.createElement('section');
section.innerHTML = 'section';
document.getElementById('box').appendChild(section);
同じ理由でjQueryのappendがダメになるみたい。
$('#box').append('<section>section</section>');
こうすればいけるっぽい。
$('#box').append( $('<section>').text('section') );
あと、innershiv.jsというのがあって、これを使うといけるみたい。
var div = document.createElement('div');
div.appendChild( innerShiv('<section>section</section>') );
document.getElementById('box').appendChild(div);
jQueryのappendもおk。
$('#box').append( innerShiv('<section>section</section>') );
- Prev Entry
- jstudy #1 を開催しました。
- Next Entry
- はてブのWeb HookでDeliciousと同期する
コメントフォーム
スパムがひどいので一時的にコメントフォーム閉じます。ゴメンナサイ。
