1

私の最後のスコープは、一度クリックされた入力フィールドのテキストを変更し、その結果、<div>コンテナーをコンテナーに置き換えること<form>です。

ただしフォームの作成後、クリック イベントは発生しなくなります

このjfiddleでは、フォームの作成と div の発火を確認する関数のみを投稿しました。何かご意見は?

(また、コードの読みやすさと効率を改善するための提案やコメントは常に受け入れられます。私は初心者です)。

ステファノ

コード: HTML

<body>
    <div id="pubbl">
        <div id="title"><h1>A sentence</h1></div>
        <div id="content">“Age is my alarm clock,” the old man said. “Why do old men wake so early? Is it to have one longer day?”
“I don’t know,” the boy said. “All I know is that young boys sleep late and hard”. Ernest Hemingway.</div>
    </div>
</body>
<button id="btn">show html</button>
<button id="cr">create form</button>

コード: JavaScript

$("#btn").click(function(){
    alert($("body").html());
    alert(there_is_form());
});

$("#cr").click(function(){
    crea_form();
});

$("#title").click(function(){
    alert("title fired");
});
$("#content").click(function(){
    alert("content fired");
});

function crea_form(){
    alert("function create form fired");
    if (there_is_form()) { return; };
    var test_div = document.getElementById('pubbl');
    var cont = test_div.innerHTML;
    test_div.innerHTML = '';    
    var form = test_div.appendChild(document.createElement('form'));
    form.name = 'input';
    form.action = 'target-etc';
    form.method = 'post';
    form.innerHTML = cont;
    input = form.appendChild(document.createElement('input'));
    input.type = 'submit';
    input.value = 'Submit';
};

function there_is_form(){
    return document.getElementsByTagName("form").length > 0;
};
4

1 に答える 1