如何在jQuery Mobile中动态设置'data-collapsed'和'data-theme'? [英] how to dynamically set the ' data-collapsed' and 'data-theme' dynamically in jQuery Mobile?

查看:114
本文介绍了如何在jQuery Mobile中动态设置'data-collapsed'和'data-theme'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时动态设置'data-theme'和'data-collapsed'时出现问题,我用过:

i have a problem in setting the 'data-theme' and 'data-collapsed' dynamically at run time, i used:

$(selector).attr('data-collapsed',false) 

$(selector).attr('data-theme',b) 

但它不起作用,如何使用jQuery或javascript解决这个问题?

but it doesn't work, how to solve this problem using jQuery or javascript?

推荐答案

你可以使用pagebeforecreate事件

You can do the with pagebeforecreate event

  • http://jquerymobile.com/test/docs/api/events.html

请注意,通过绑定到pagebeforecreate,您可以在jQuery Mobile的默认窗口小部件自动初始化之前操作标记
。对于
示例,假设您要在HTML源代码中通过JavaScript而不是
添加数据属性,这是您要使用的事件。

Note that by binding to pagebeforecreate, you can manipulate markup before jQuery Mobile's default widgets are auto-initialized. For example, say you want to add data-attributes via JavaScript instead of in the HTML source, this is the event you'd use.

示例:

  • http://jsfiddle.net/ayEWB/

JS

$('#home').live('pagebeforecreate',function(event) {
    var col = $('#collapseMe');

    // Alternative settings
    //col.attr('data-collapsed','false');
    //col.attr('data-theme','b');

    col.data('collapsed',false);
    col.data('theme','b');
});

HTML

<!DOCTYPE html>
<html class="ui-mobile-rendering">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery Mobile: Demos and Documentation</title>
    <link rel="stylesheet"  href="http://jquerymobile.com/test/css/themes/default/jquery.mobile.css" />

    <script src="http://jquerymobile.com/test/js/jquery.js"></script>
    <script src="http://jquerymobile.com/test/js/jquery.mobile.js"></script>

</head>
<body>
<div data-role="page" id="home">
    <div data-role="content">
        <div data-role="collapsible" data-theme="a" data-content-theme="a" id="collapseMe">
           <h3>Header swatch A</h3>
           <p>I'm the collapsible content with a themed content block set to "A".</p>
        </div>
    </div>
</div>
</body>
</html>
​

这篇关于如何在jQuery Mobile中动态设置'data-collapsed'和'data-theme'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆