1

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 パラメータもうまく機能しています。しかし、何らかの理由でボタンとして表示されず、通常の 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);
});
4

1 に答える 1

2

を使用trigger('create')して、すべてのjQueryMobileフォーマットを適用できます。完全に機能するコード:

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/

于 2012-09-17T17:24:18.267 に答える