LoginSignup
7
7

More than 5 years have passed since last update.

jQuery Mobileでボタンやリストビューを動的に追加する

Last updated at Posted at 2012-08-01

jQuery Mobileが初期化処理を完了し、ページが表示されたあとに

<a data-role="button"/>

を追加してもただのanchor要素が追加されるだけだった。以下のようにbutton関数を呼べばボタンになった。
参考: http://forum.jquery.com/topic/how-to-dynamically-add-data-role-button

<script>
(function($){
  $(window).load(function(){
    var $content = $('#content');
    $('<a/>').text('Label')
      .data('icon','home')
      .appendTo($content)
      .button()
      ;
  });
})(jQuery);
</script>

同じようにリストビューも追加できた。ただし、こちらはappendしたあとに呼び出さないとうまく表示されなかった。

$('<ul/>').append('<li>aaa</li><li>bbb</li>')
  .appendTo($content)
  .listview()
  ;

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7