1

ボタンをクリックするだけで、順序付けられていないリストを動的に作成し、項目を追加しています。contenteditable 属性が true に設定されているセクションにこれを追加します。しかし、私はそれが機能しているとは思いません。リストでも contenteditable 属性を true に設定しましたが、追加先のセクションからそれを継承することになっていると思います。これが私がやっていることのコードです。

// create text input
var categoryInput = document.createElement('input')
// create button to add the text entered to a list
var btnAddToList = document.createElement('input');
btnAddToList.type ="button";
//create a section to add a list to 
var section =  document.createElement('section');
var ul=document.createElement('ul');
section.appendChild(ul);
section.contenteditable = "true";
ul.contenteditable = "true";
//create an event handler to add to the list
if (btnAddToList.addEventListener) {   btnAddToList.addEventListener('click', function () { addToList(ul, categoryInput.value);});
} else if (btnAddToList.attachEvent) {
btnAddToList.addEvent('click', function () { addToList(ul, categoryInput.value);});

これが私が呼び出す関数です

function addToList(unorderedlist, inputText) {

    if(inputText.length == 0) {
        alert("Add Text");
        return;
    }   


    var listitem = document.createElement('li');
    var listvalue = document.createTextNode(inputText);
    listitem.appendChild(listvalue);
    unorderedlist.appendChild(listitem);
}

私が間違っていること、またはしていないことは何ですか? どんな助けでも感謝します。ありがとう

4

2 に答える 2

3

プロパティはcontentEditable(大文字の「E」に注意) であり、 ではありませんcontenteditable

section.contentEditable = "true";
于 2013-10-23T08:18:01.257 に答える