我正在尝试使用 add() 构建一个 jQuery 集,但我的实现并未向该集中添加元素。
var questionPath = $(); // create empty set
var selectedRuleElement = $(event.currentTarget);
questionPath.add(selectedRuleElement); //no element is added to set here!
selectedRuleElement.find('li.property-value-node').each(function () {
var questionId = $(this).attr('data-parent-id');
questionPath.add($('#' + questionId)); // or here!
});
return questionPath;
上面的两行 add() 行都给出了一个有效元素作为参数。有人可以告诉我向 jQuery 集合添加元素的正确方法吗?
最佳答案
与字符串一样,jQuery 对象是不可变的,因此您需要重新分配返回值。否则,您将丢弃 .add()
返回的值,并且 questionPath
保持不变。
questionPath = questionPath.add(selectedRuleElement);
关于jQuery Add() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11636336/