1

我使用 HTML 和 Javascript 制作了一个基本的 XML 格式化程序。我想为此添加一个删除功能。基本上,我希望能够删除一个条目,但不清除或破坏任何其他数据。

<contact
<!--John Doe-->
 first_name="John"
 last_name="Doe"
 contact_type="sip"
 account_id="104"
 subscribe_to="sip:104@10.10.1.24"
 has_voicemail="1"
 can_monitor="1"
>
<numbers>
 <number dial="1064" dial_prefix="" label="Extension" />
 <number dial="555-0123" dial_prefix="718" label="Work Line" primary="1" />

我的想法是找到包含 John Doe 的联系人标签并从<contactto删除</numbers>

`indexOf() 是否可以通过包含某些信息来删除该组。

对于上下文:我向 plunkr 添加了一个演示。这需要表单数据并将其导出到文本区域

http://run.plnkr.co/plunks/b9QKZ7KZP0IlcyeCTTc9/

4

1 回答 1

0

I think you might try a other way.

  1. Create some Javascript Objects to hold the data.
  2. Present objects to that textarea.
  3. When you add/remove a data, operate the objects first, then re-present.

Code looks like

//Contact list.
var contacts = [];

// Contact class.
function Contact() {
    ...
    this.name = "";
    this.numbers = [];// numbers
    ...
}

Contact.prototype.toString = function(){
    return "<contact name=\"" + this.name + ...;
};

// Add new Contact
function addContact(params) {

    var contact = new Contact();
    // set properties
    // contact.name = name;
    contacts.push(contact);
    showContacts();
}

// Add new Contact
function deleteContact(name) {

    for (var i = contacts.length-1; i >= 0; i--) {
        if (contacts[i].name == name) {
            contacts.splice(i, 1);
            return;
        }
    }
}

// present contacts
function showContacts(){
    var text = "";
    for(var c in contacts){
        text += c.toString();
    }
    textarea.value = text;
}

// other functions like addNumber etc.

Code will become a little complicate, but more clear and flexible.

于 2013-07-04T12:42:04.397 回答