如何在append()之后或before()之后重置?请帮忙。提前谢谢了。
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
Run Code Online (Sandbox Code Playgroud)
您的代码示例使用.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');?????????
Run Code Online (Sandbox Code Playgroud)
请记住,使用.before()不放置任何物品里面的#tag_id,而是放在它面前的#tag_id。
如果您打算将内容放进 去,#tag_id但一开始就要使用.prepend()。
如果您的代码使用.append(),则一种选择是保留对您要添加的元素的引用,然后使用该引用将其删除。
var $appendElem = $('<div>some appended element</div>');
$appendElem.appendTo('#tag_id');
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过相同的引用将其删除。
$appendElem.remove();
// or
$appendElem.detach();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26983 次 |
| 最近记录: |