我有一个具有无限滚动的页面,当用户向下滚动时,它会追加新元素。“.产品-变异”就是其中一个要素。我想删除所有新到达的元素的第一个选项。下面的代码做了这个工作,但是我认为它在这个过程中增加和下降了DOM太多的延迟性能。是否有一种更好和更有效的方法来消除仅仅是新到的元素的第一个选择?
所有新到达的元素的第一个选项总是'‘,这就是我试图删除的内容。
jq(".product-variations").each(function(){
var fresh_variant = jq(this).find("option:nth-child(1)").val();
if(fresh_variant == '')
{
jq(this).find("option:nth-child(1)").remove();
}
jq(this).trigger('change');
});发布于 2015-11-01 14:04:10
我会插一面旗子
jq(".product-variations:not([data-set])").each(function(){
var fresh_variant = jq(this).find("option:nth-child(1)").val();
if(fresh_variant == '')
{
jq(this).find("option:nth-child(1)").remove();
}
jq(this).trigger('change');
jq(this).attr('data-set',1);
});所以现在,当您添加item时,您设置了一个标志" set“。
下一次,你只扫描新的。(谁没有国旗)
哦,这里有一个更好的代码版本:
jq(".product-variations:not([data-set])").each(function(){
var $this=jq(this);
var $opt=$this.find("option:nth-child(1)");
var fresh_variant = $opt.val();
if(fresh_variant == '')
{
$opt.remove();
}
$this.trigger('change');
$this.attr('data-set','whatever');
});https://stackoverflow.com/questions/33462944
复制相似问题