Jquery在没有id或class的div之后选择文本

时间:2014-12-31 09:14:25

标签: jquery

这是我的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();

但这不起作用

3 个答案:

答案 0 :(得分:4)

如果您要删除所有内容(包括<br />):

var start = document.getElementById('errorZone');
while (start.previousSibling) {
    start.parentNode.removeChild(start.previousSibling);
}

&#13;
&#13;
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;
&#13;
&#13;

答案 1 :(得分:0)

$('#errorZone').closest('div:not('.errorZone')').remove();

答案 2 :(得分:0)

可能是您可以使用.parent()而不是.prev()。看到这个链接: - How to get the text of parent element using jquery?