简体   繁体   中英

JQuery Mobile: Style dynamically created elements

I am using JQuery to create two buttons:

      mapbutton = '<a class="mapbtn" rel="external" data-role="button" href="map.html?longlat='+ coords +' ">Map</a>';
      $('.event').append(mapbutton);

      var input = '<a href="#" data-role="button" class="save_event">Save to Planner</a>';
      $('.event').append(input);

I have added the required data-role="button" to get JQuery Mobile to style them but still then appear just as normal links.

I am guessing this is because JQM styles the DOM elements before the scripts run.

Would anyone know how to alter this so that JQM styles these dynamically created elements as well?

You can invoke button widget on the dynamically created element to appear as JQM button widget using .button()

var mapbutton = '<a class="mapbtn" rel="external" data-role="button" href="map.html?longlat=' + coords + ' ">Map</a>';

$('.event').append($(mapbutton).button());

var input = '<a href="#" data-role="button" class="save_event">Save to Planner</a>';
$('.event').append($(input).button());

Demo

See Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM