jQuery Mobile,按钮无法在动态列表中正确呈现

时间:2012-07-04 14:54:35

标签: jquery listview button jquery-mobile

  

可能重复:
  Refresh a section after adding HTML dynamically to jquery mobile

我正在尝试在jQueryMobile页面中插入动态列表。

在我的主页面中,我得到了一个名为“list”的HTML ul元素:<ul id='list' data-role='listview'> </ul>

每个列表元素都包含一个网格(class="ui-grid-a")。网格中的第一个div,每个元素(div class="ui-block-a")包含一个h2描述,而第二个包含三个水平排列的按钮,位于一个控制组(div class="ui-block-b" data-role="controlgroup" data-type="horizontal")中。

当元素在静态页面中时,按钮呈现完美呈现,但如果我尝试在页面中将它们以动态方式插入,则它们会呈现不正确。下面是我的代码(jsfiddle here)。我错过了什么?

DOMElement += '<li>';
        for (i = 0 ; i < length ; i++) {
            if (typeof systemArray[i] !== 'undefined') {
                DOMElement += '<fieldset class="ui-grid-a"> <div class="ui-block-a" style="width:60%;"> <h2 id="'
                DOMElement += "system" + systemArray[i].name;
                DOMElement += '">';
                DOMElement += systemArray[i].name;
                DOMElement += '</h2>';
                DOMElement += '</div>';
                DOMElement += '<div class="ui-block-b" data-role="controlgroup" data-type="horizontal" style="width:30%;">';
                DOMElement += '<a href="#" class="deletebutton" data-icon="delete" data-role="button" id="';
                DOMElement += 'delete|' + systemArray[i].name;
                DOMElement += '" data-iconpos="notext">Cancel</a>';
                DOMElement += '<a href="#" class="modifybutton" data-icon="gear" data-role="button" id="';
                DOMElement += 'modify|' + systemArray[i].name;
                DOMElement += '" data-iconpos="notext">Modify</a>';
                DOMElement += '<a href="#" class="connectbutton" data-icon="arrow-r" data-role="button" id="';
                DOMElement += 'connect|' + systemArray[i].name;
                DOMElement += '" data-iconpos="notext">Connect</a>';
                DOMElement += '</div>'
                DOMElement += '</fieldset>';
            }   

        }
        DOMElement += '</li>';

        // Needs trigger ('create') to create the icons http://jquerymobiledictionary.pl/faq.html
        $("#list").html(DOMElement).trigger('create');
        $("#list").listview("refresh");

3 个答案:

答案 0 :(得分:1)

检查你的小提琴我发现问题是控制组中的按钮自己堆叠。你可以使用css解决这个问题:

.ui-block-b a{
    margin-left:6px !important;
}

小提琴:

fiddle

我不知道为什么jqm这样做,它经常发生在开发时,有时会在标题上列出堆栈15px。然而,css解决方法并不难做到并且工作正常。如果你不希望每个ui-block-b都有这个边距,你可以轻松地为你的按钮添加一个额外的类,并将css添加到它。

IMO处理此问题的最佳方法是使用所有jqm怪癖创建一个单独的特定css样式表,这样您就可以轻松处理和可视化它们。

答案 1 :(得分:1)

替换:

DOMElement += '" data-iconpos="notext">.....</a>';

使用:

DOMElement += '" data-iconpos="notext">.....</a>\n';

答案 2 :(得分:0)

从链接中删除data-role =按钮属性。当你拥有它时,你试图在彼此之上初始化两个小部件,这会产生冲突。

相关问题