简体   繁体   English

JavaScript删除功能不起作用(绝对是新手)

[英]JavaScript delete-function does not work (absolute Newbie)

First off: sorry for that awful Code. 首先,对不起这个糟糕的代码。 I am an absolute beginner and this is my very first JS code ever and I had to code it under time-pressure (not good). 我是一个绝对的初学者,这是我有史以来第一个JS代码,我不得不在时间压力(不好)下对其进行编码。

The task was to code a guest book. 任务是编写留言簿。 I have a function that creates a new div per entry and appends it to the last one (this part works well). 我有一个函数,可以为每个条目创建一个新的div并将其附加到最后一个(此部分效果很好)。

var lastid = 2;

function eintragen() {

    ...

    var divC = document.createElement('div');

    var ident='item'+lastid;

    divC.innerHTML = '<table id="'+ident+'" class="eintrag center">  
    <tr><td>Name:</td><td>'+name2+'</td></tr><tr>
    <td>Email:</td><td>'+email2+'</td></tr> 
    <tr><td>Eintrag</td><td>'+nachricht2+'</td></tr>
    <tr><td></td><td><button class="buttonklein"value="loeschen"

    ***onclick="loeschen('+ident+')"***

    >Eintrag löschen</button></td></t></table><hr>';



    **divC.setAttribute('id',ident);**

    var add = document.getElementById('item1');
    add.appendChild(divC);


    lastid+=1;

    ...

}  

I tried do highlight the relevant part. 我试过突出相关部分。 As you can see, I gave the id to the divC twice, because after testing, I figured out, that it did not work in the "divC.innerHTML"-segment. 如您所见,我将ID两次分配给divC,因为经过测试,我发现该ID在“ divC.innerHTML”段中不起作用。

Every entry has an own delete-button, which should - you guess it - delete that associated entry.After clicking the button, the function starts, but then nothing happens. 每个条目都有一个自己的删除按钮,应该-可以猜到-删除相关的条目。单击该按钮后,该功能将启动,但随后什么也没有发生。

function loeschen(itemid) {

   var sitem = document.getElementById(itemid); 
   sitem.parentNode.removeChild(sitem);
}

I guess, since assigning the ID to the divC did not work in the "divC.innerHTML" part, it also does not work to assign it as a parameter to the "onclick" -attribute. 我猜想,由于将ID分配给divC在“ divC.innerHTML”部分中不起作用,因此也无法将其作为参数分配给“ onclick”-属性。

If I replace itemid with something concrete like 'item4' in the loeschen-function, it does delete entry number 4, so I guess the mistake is in the divc.innerHTML-part. 如果我在loeschen函数中用诸如“ item4”之类的具体内容替换itemid ,它将删除条目4,因此我猜错误出在divc.innerHTML-part中。

Question: Where did I go wrong? 问题:我哪里出问题了? Any wrong '' oder ""? 是否有错误的“奇怪”? How can i give every button the current itemid? 如何为每个按钮提供当前的itemid? everything worked fine for the other variables like name oder email, so I have absolutely no clue. 一切对于其他变量(如名称oder email)都可以正常工作,所以我完全不知道。

Thanks so much in advance and sorry for my bad style. 在此先非常感谢,并为我的糟糕作风表示歉意。 I will work on this in the future. 以后我会努力的。

User Teemu found the answer super fast and was a great help. 用户Teemu很快找到了答案,这是一个很大的帮助。 It was indeed bad syntax in the divC.innerHTML-segment. 在divC.innerHTML段中,这确实是语法错误。 This does the job: 这样就可以了:

    divC.innerHTML = `<table id="${ident}" class="eintrag center">  
    <tr><td>Name:</td><td>${name2}</td></tr><tr>
    <td>Email:</td><td>'+email2+'</td></tr> 
    <tr><td>Eintrag</td><td>${nachricht2}</td></tr>
    <tr><td></td><td><button class="buttonklein" value="loeschen"
    onclick="loeschen('${ident}')">Eintrag löschen</button>
    </td></t></table><hr>`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM