这是我的HTML代码:
<div>
Please complete this form to create a new account.
<br>
Fields marked with (*) are required.
<div id="errorZone"></div>
</div>
我想在div和div之间选择文本id =“errorZone”。
我正在尝试这段代码:
$('#errorZone').prev().text().remove();
但这不起作用
答案 0 :(得分:4)
如果您要删除所有内容(包括<br />
):
var start = document.getElementById('errorZone');
while (start.previousSibling) {
start.parentNode.removeChild(start.previousSibling);
}
var start = document.getElementById('errorZone');
while (start.previousSibling) {
start.parentNode.removeChild(start.previousSibling);
}
&#13;
<div>
Please complete this form to create a new account.
<br>Fields marked with (*) are required.
<div id="errorZone">Text in 'errorZone.'</div>
</div>
&#13;
答案 1 :(得分:0)
$('#errorZone').closest('div:not('.errorZone')').remove();
答案 2 :(得分:0)
可能是您可以使用.parent()而不是.prev()。看到这个链接: - How to get the text of parent element using jquery?