如何在append()或before()之后重置?请帮忙。提前谢谢了。jquery reset append()
if((#tag_id).text() == 'something'){
$(#tag_id).before("x");
}
// I want to reset the #tag_id to empty
$(#tag_id).val(''); // this doesn't work
如何在append()或before()之后重置?请帮忙。提前谢谢了。jquery reset append()
if((#tag_id).text() == 'something'){
$(#tag_id).before("x");
}
// I want to reset the #tag_id to empty
$(#tag_id).val(''); // this doesn't work
您的代码示例使用.before()
。在这种情况下,如果.before()
被给定了围绕x
的元素而不仅仅是文本,那将会更好。
然后,你可以这样做:
// Wrap the "x" that you're inserting with <span> tags
$('#tag_id').before('<span>x</span>');
// Use .prev() to reference the new <span> and remove it
$('#tag_id').prev().remove('span');
请记住,使用.before()
不放置任何物品内的#tag_id
,而是会将它的#tag_id
之前。
如果您的意思是内容为里面#tag_id
但在开始时,您将使用.prepend()
。
如果您的代码使用.append()
,一种选择是保留给你附加的元素的引用,并使用以后将其删除。
var $appendElem = $('<div>some appended element</div>');
$appendElem.appendTo('#tag_id');
然后,您可以通过相同的参考删除它。
$appendElem.remove();
// or
$appendElem.detach();
如果你想删除它里面的HTML,使用方法:
$('#tag_id').html('');
如果你想删除id
属性,使用removeAttr
:
$('#tag_id').removeAttr('id');
更多信息:
这是你想要的吗?
$("#tag_id").html("");