【问题标题】:Jquerymobile create button using javascriptJquerymobile使用javascript创建按钮
【发布时间】:2012-09-17 17:21:41
【问题描述】:

我正在尝试使用应该具有 data-role="button" 的 javascript 创建 href。但是按钮没有显示。这是我的代码。

var a = document.createElement("A");
a.appendChild(document.createTextNode("Skúsiť Znova"));
a.setAttribute("onClick","checkConnection();");
a.setAttribute("data-role","button");
a.setAttribute("data-inline","true");
a.setAttribute("data-corner","false");

我将它作为孩子附加到 div 中,我可以看到文本,而且 onClick 参数也很好用。但由于某种原因,它没有显示为按钮,而是显示为 normaln href。关于如何使用 JS 动态创建 jquerymobile 按钮的任何建议?这是完整的代码:

$(document).ready(function(){
    var a = document.createElement("A");
    var div = document.createElement("div");
    var span = document.createElement("span");
    var p2 = document.createElement("p");
    var p = document.createElement("p");
    a.appendChild(document.createTextNode("Skúsiť Znova"));
    a.setAttribute("onClick","checkConnection();");
    a.setAttribute("data-role","button");
    a.setAttribute("data-inline","true");
    a.setAttribute("data-corner","false");
    div.setAttribute("id","alertMessage");
    span.appendChild(document.createTextNode("Pripojenie zlyhalo"));
    p2.setAttribute("style","text-align: center;");
    span.setAttribute("class","red");
    p.appendChild(span);
    p.appendChild(document.createTextNode(" (skontrolujte nastavenia siete)."));    
    div.appendChild(p);
    p2.appendChild(a);
    div.appendChild(p2);
    var mainDiv = document.getElementById("alert");
    mainDiv.appendChild(div);
    $('mainDiv').trigger('create');
    var toppos=($(window).height()/2) - ($("#alertMessage").height()/2);
    var leftpos=($(window).width()/2) - ($("#alertMessage").width()/2);
    $("#alertMessage").css("top", toppos).css("left",leftpos);
});

【问题讨论】:

    标签: javascript button jquery-mobile


    【解决方案1】:

    您可以使用trigger('create') 来应用所有的jQuery Mobile 格式。完整的工作代码:

    HTML

    <div id="container"></div>
    

    JavaScript

    var a = document.createElement("A");
    a.appendChild(document.createTextNode("Skúsiť Znova"));
    a.setAttribute("onClick","checkConnection();");
    a.setAttribute("data-role","button");
    a.setAttribute("data-inline","true");
    a.setAttribute("data-corner","false");
    
    $('#container').append(a).trigger('create');
    

    这可以在此处看到:http://jsfiddle.net/sQ2dA/


    响应您的编辑:问题是您在引号中添加了mainDiv,因此您在这一行传递了一个字符串:

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

    但是你应该传递变量mainDiv:

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

    请在此处查看工作示例:http://jsfiddle.net/P62Cp/

    【讨论】:

    • 我应该把它放在哪里?在代码的末尾(提供)?因为如果是,它仍然无法正常工作。
    • ty,我已经更新了我的代码(整个代码),正如您在编辑中看到的那样,但它不起作用,您能检查一下吗?
    • 使用我提供的整个代码我可以看到文本,但仍然没有按钮。
    • @horin:问题是您在将元素插入 DOM 之前调用了trigger('create')。您应该能够将行 mainDiv.appendChild(div); 更改为 mainDiv.appendChild(div).trigger('create'); 或在其后加上 $('mainDiv').trigger('create');
    • 好的,我已经更改了代码(正如您在编辑中看到的那样)并放入 $('mainDiv').trigger('create');之后,但它仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多