创建文档片段、向其附加元素并返回该片段时出现错误。我过去使用过文档片段(在此示例之外滥用它们)并且从未遇到此错误。在开发人员控制台中,它将此列为错误来源:
fragment.appendChild($balanceNumber, $balanceSign);
错误
Uncaught NotFoundError:试图在不存在的上下文中引用节点。
功能
var constructBalanceFragment = function(balance) {
var $balanceNumber, $balanceSign, fragment, sign;
fragment = document.createDocumentFragment();
$balanceNumber = $('<span class="balance_number"></span>');
$balanceSign = $('<span class="balance_sign"></span>');
if (balance > 0) {
sign = "+";
$balanceSign.addClass("positive_number");
} else if (balance < 0) {
sign = "-";
$balanceSign.addClass("negative_number");
} else {
sign = "";
}
$balanceNumber.text(balance);
$balanceSign.text(sign);
fragment.appendChild($balanceNumber, $balanceSign);
return fragment;
编辑
我已经尝试过了,但在有人问之前仍然遇到同样的错误
fragment.appendChild($balanceNumber);
fragment.appendChild($balanceSign);