删除子项不起作用
我在使用 javascript removeChild 函数时遇到问题。 这是我的脚本:
////function to add element by ID////
var i=1;
$("#buttonAdd").live('click',function() {
$("#list1 li:last-child").parent().append('<li>'+
'<label for=njajal[]>njajal'+
'<textarea class="tinymce" name="njajal[]" id="aaa'+i+'"></textarea>'+
'<span><a class="delIt" id="'+i+'"><b>Hapus</a></span></label>'+
'</li>');
tinyMCE.execCommand('mceAddControl', false, 'aaa'+i);
console.log('add '+i);
i++;
});
////Function to delete element by ID/////
function delIt(eleId)
{
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('njajal');
parentEle.removeChild(ele);
}
有什么问题吗?
HTML 代码如下:
<div id="form">
<form method="post" action="">
<fieldset>
<ol id="list1">
<li>
<label for="njajal[]">njajal
<textarea name="njajal[]" class="tinymce" ></textarea>
</label>
</li>
</ol>
<div id="addOpt">
<a id="buttonAdd" class="bt"><b>Tambah</a>
</div>
</fieldset>
</form>
</div>
屏幕截图:
I have a problem with the use of the javascript removeChild function.
Here's my script:
////function to add element by ID////
var i=1;
$("#buttonAdd").live('click',function() {
$("#list1 li:last-child").parent().append('<li>'+
'<label for=njajal[]>njajal'+
'<textarea class="tinymce" name="njajal[]" id="aaa'+i+'"></textarea>'+
'<span><a class="delIt" id="'+i+'"><b>Hapus</a></span></label>'+
'</li>');
tinyMCE.execCommand('mceAddControl', false, 'aaa'+i);
console.log('add '+i);
i++;
});
////Function to delete element by ID/////
function delIt(eleId)
{
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('njajal');
parentEle.removeChild(ele);
}
What is the problem?
Here's the HTML code:
<div id="form">
<form method="post" action="">
<fieldset>
<ol id="list1">
<li>
<label for="njajal[]">njajal
<textarea name="njajal[]" class="tinymce" ></textarea>
</label>
</li>
</ol>
<div id="addOpt">
<a id="buttonAdd" class="bt"><b>Tambah</a>
</div>
</fieldset>
</form>
</div>
Screnshot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在第一个函数中使用 jQuery,因此删除该元素的最简单方法是使用 jQuery:
以下是使用纯 JavaScript 完成相同操作的方法:
You use jQuery in your first function, so the easiest way to remove that element would be with jQuery:
Here's how you can accomplish the same thing with plain javascript:
编辑:
为了让事情更简单,请将
>
读为“子项:据我所知,问题在于
textarea >” label > li > ol
唯一实际具有 id 的元素是因此要删除标签(如图所示),请将 delIt 更改为:
旧答案:
因为我们看不到 HTML 我我不确定问题是什么,除了 Marc B 提到的“njajal”不是 eleID 的父级之外,要解决这个问题,我建议:
EDIT:
To make things simpler read
>
as "child of:From what I can tell the problem is that
textarea > label > li > ol
. The only element actually having an id is<ol>
so to remove the label (as you show in the image) change delIt to:Old Answer:
As we cannot see the HTML I am not certain what the problem is other than as Marc B has mentioned that 'njajal' is not the parent of eleID. To fix that I would recommend:
到目前为止提出的解决方案将不起作用,因为tinymce不是文本区域!
Tinymce 使用指定的 html 元素(在本例中为文本区域)的内容进行初始化。初始化后,将创建一个具有可编辑内容的 iframe,用户可以在其中编辑 html 内容。
为了摆脱tinymce编辑器,您需要首先将其关闭:
其次,您可以删除诸如
label
和textarea
之类的初始html元素,作为您的其他解决方案问题展示。我只是想知道为什么你的页面上有两个tinymce编辑器?
对我来说,您可能更愿意只初始化其中一个,而不是之后删除第二个。
The solutions presented till now won't work because tinymce IS NOT the textarea!!
Tinymce gets initialized using the content of a specified html-element - a textarea in this case. After initialization an iframe with a contenteditable body is created where users may edit html content.
In order to get rid of the tinymce editor you need to shut it down first:
Second, you may remove the initial html elements like
label
andtextarea
as the other solutions to your question show.I just wonder why you have have two tinymce editors on your page?
For me it looks like you might prefer to initialize just one them instead of removing the second one afterwards.