gpt4 book ai didi

javascript - .removeChild() 不工作

转载 作者:行者123 更新时间:2023-11-29 19:31:29 25 4
gpt4 key购买 nike

我已经创建了一个可以添加的表单域,但我无法删除它。不确定为什么它不起作用,我想也许我传递了错误的参数?我尝试了很多变化。

<h3 class="color-text">What key fatures are you looking for?</h3>
<p class="contact-message">
<textarea id="client-needs" placeholder="What key features are you looking to have within your website? ie. storefront, social media intigrtion, a blogging platform, content managment system, ect." name="client-needs" rows="5" cols="10"></textarea>
</p>


<script type="text/javascript">
var i = 1;
function addUrl(){
if (i<3) {
i++;
var p = document.createElement('p');
p.innerHTML = '<hr><textarea id="client-needs" placeholder="Add URL #'+i+'" name="client-needs'+i+'" rows="1" cols="10"></textarea><input type="button" value="Remove added URL" onclick="removeUrl(this)">';
document.getElementById('examples').appendChild(p);

}
}

function removeUrl(textarea){
document.getElementById('client-needs').removeChild(textarea.parentNode);
i--;
}
</script>

<h3 class="color-text">Do you have and examples of what you want?</h3>
<p class="contact-message" id="examples">
<textarea id="examples" placeholder="Do you know of any website like the one you want? ie. google.com, p5services.com, youtube.com" name="examples" rows="1" cols="10"></textarea>
</p>

<br>

<input type="button" value="Add url example" onclick="addUrl()">

最佳答案

removeChild 的使用不正确。要删除添加的带有内容的段落,你应该这样做:

function removeUrl(textarea) {
textarea.parentNode.parentNode.removeChild(textarea.parentNode);
i--;
}

textarea.parentNode 是要删除的 p 元素,而 textarea.parentNode.parentNode 是这个段落的父元素,不管它是什么. removeChild 应该在父级上调用,它需要父级的一个子级才能删除。

演示:http://jsfiddle.net/7caysjw8/

关于javascript - .removeChild() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27207365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com