1

My mobile website dynamically adds the header bar to reduce code redundancy.

However, I'm stuck with styling header section of a jquery mobile page.

When I see the generated HTML tags, it looks okay,

but its element is not decorated by jQuery Mobile.

After adding the content, I invoked

$(pageId).trigger('create');

Do you have any ideas?

Sungam Yang
  • 595
  • 3
  • 7
  • 16

2 Answers2

1

This worked for me : http://jsfiddle.net/emBxx/2/

HTML:

<script type="text/javascript" src="js/main.js"></script>

...

<div data-role="page" data-theme="b">
    <header></header>
    <div data-role="content">
        <div class="content-primary">
            <br />
            <ul data-role="listview" data-filter="true">
                <li><a href="index.html">Some</a></li>
                <li><a href="index.html">random</a></li>
                <li><a href="index.html">searchable</a></li>
                <li><a href="index.html">content</a></li>
                <li><a href="index.html">(list!)</a></li>
            </ul>
        </div><!--/content-primary -->  
    </div>
    <footer></footer>
</div><!-- page end-->
<script>      
    appendJQMHeader('Injected header !'); 
    appendJQMFooter('—Injected ftr!', 'JQM 1.3.1Beta');
</script>

JS, in js/main.js:

function appendJQMHeader(pageTitle) {
    $('header').replaceWith(
        '<header data-role="header" data-theme="f">'+
        '<h1>'+pageTitle+'</h1>'+
        '<a href="index.html"  data-transition="slide" data-rel="back" data-icon="home" data-iconpos="notext" data-ajax="true">Home</a>'+
        '</header><!-- /header -->');
}

function appendJQMFooter(left, right) {
    $('footer').replaceWith('<footer data-role="footer" data-theme="f" class="jqm-footer"><p>&copy;'+left+'!</p><p class="jqm-version">—'+right+'</p></footer>');
}

Note: for JSfiddle, it require 'Framework & extension > No wrap - in <head>'
For stand alone version, it worked fine with html head calling js/main.js containing the JS. Then html body with appendJQMHeader() & appendJQMFooter(). See the fiddle :)

Hugolpz
  • 17,296
  • 26
  • 100
  • 187
0

Instead of invoking the trigger method, I executed the below command, and it works well.

$('#pageHome').closest(":jqmData(role='page')").trigger('pagecreate');
Sungam Yang
  • 595
  • 3
  • 7
  • 16