【问题标题】:How does one render jQuery Mobile UI Object post page load如何渲染 jQuery Mobile UI 对象后页面加载
【发布时间】:2011-06-08 01:30:06
【问题描述】:

我有一些在客户端使用 JS 生成的 HTML。我仍然想将 jQuery Mobile UI 的样式和功能也应用于这些对象。我似乎无法弄清楚如何...

假设我生成了一个:

<div data-role="fieldcontain">
    <label for="select-choice-1" class="select">Choose shipping method:</label>
    <select name="select-choice-1" id="select-choice-1">
        <option value="standard">Standard: 7 day</option>
        <option value="rush">Rush: 3 days</option>
        <option value="express">Express: next day</option>
        <option value="overnight">Overnight</option>
    </select>
</div>

并且想通过页面内的 jQuery Mobile UI 呈现它......如何做到这一点?

我知道使用标准 jQuery UI 我只需要按照以下方式进行调用:

$("#select-choice-1").buttonset();

jQuery Mobile UI 有类似的东西吗?

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-mobile


    【解决方案1】:

    如果在页面创建时调用

    $('#page').live('pagecreate',function(event){
        $('#elem_container').page();
    });
    

    您进行递归循环。我编写了自己的 enhance() 函数来解决这个问题。

    $('#page').live('pagecreate',function(event){
        enhance($('#elem_container'));
    });
    

    函数基本上只是把下面的代码浓缩...

    function enhance(dis){
        dis.find( "[type='radio'], [type='checkbox']" ).checkboxradio();
        dis.find( "button, [type='button'], [type='submit'], [type='reset'], [type='image']" ).not( ".ui-nojs" ).button();
        dis.find( "input, textarea" ).not( "[type='radio'], [type='checkbox'], button, [type='button'], [type='submit'], [type='reset'], [type='image']" ).textinput();
        dis.find( "input, select" ).filter( "[data-role='slider'], [data-type='range']" ).slider();
        dis.find( "select:not([data-role='slider'])" ).selectmenu();
    }
    

    旧帖:

    在挖掘了 jQuery 移动文件之后,我终于弄明白了……其实很简单……

    $("#select-choice-3").selectmenu();
    

    这是我在其中挖出的代码块...

    _enchanceControls: function() {
        var o = this.options;
        // degrade inputs to avoid poorly implemented native functionality
        this.element.find( "input" ).each(function() {
            var type = this.getAttribute( "type" );
            if ( o.degradeInputs[ type ] ) {
                $( this ).replaceWith(
                    $( "<div>" ).html( $(this).clone() ).html()
                        .replace( /type="([a-zA-Z]+)"/, "data-type='$1'" ) );
            }
        });
    
        // enchance form controls
        this.element
            .find( "[type='radio'], [type='checkbox']" )
            .checkboxradio();
    
        this.element
            .find( "button, [type='button'], [type='submit'], [type='reset'], [type='image']" )
            .not( ".ui-nojs" )
            .button();
    
        this.element
            .find( "input, textarea" )
            .not( "[type='radio'], [type='checkbox'], button, [type='button'], [type='submit'], [type='reset'], [type='image']" )
            .textinput();
    
        this.element
            .find( "input, select" )
            .filter( "[data-role='slider'], [data-type='range']" )
            .slider();
    
        this.element
            .find( "select:not([data-role='slider'])" )
            .selectmenu();
    }
    

    【讨论】:

      【解决方案2】:

      更新!

      是的!带有事件的新实现刚刚落地!

      http://jquerymobile.com/blog/2011/07/22/jquery-mobile-team-update-week-of-july-18th/

      现在在 beta2 中会有一个create 事件,该事件在元素上触发将导致其呈现。

      我会在 beta2 发布时更新常见问题。

      找到您添加到 DOM 的最顶层元素并在其上调用 .page()。 这个问题实际上重复了很多其他标记为 jquery-mobile 的问题,因此我创建了一个镜头教程,不再每次都描述它。

      在此处查看第一篇文章:http://jquerymobiledictionary.dyndns.org/faq.html

      【讨论】:

      • 如果您只刷新单个组件,则可以使用该元素的特定功能。否则,您可以使用 .page() 来分析 DOM 元素并自动使用所有可用的小部件:)
      • 如果在 pagebeforecreate 上调用它会失败,这是可以理解的,如果在 pagecreate 上你会进行递归循环。我编写了自己的增强()函数来解决这个问题......在下面详细说明我的其他答案。
      • 你有没有机会提供一个例子或链接......?最好的是,如果您将其作为答案提出,我将对其进行投票并对其进行检查...
      • OMG...真是一团糟...我刚刚发布了一些不真实的东西:(该事件将不会实施,因为有一个基于已经可用的东西的解决方案。我会做一些笔记并发布它们。[删除我的评论以免混淆人们]
      • 一切顺利,一切顺利。该活动已实施,它将在 beta2 中推出! :) 请稍等。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多