简体   繁体   English

prop()花费的时间太长

[英]prop() takes too long

I have a <select> element displaying values from a big database. 我有一个<select>从一个大数据库中显示的值的元素。 Using jQuery I need to select the relevant items. 使用jQuery,我需要选择相关项目。 The problem is that when I have large amount items to select, about 60,000, the time that $(options2select).prop('selected',true) , can take too long, about 50 seconds! 问题是,当我要选择大量项目时,大约需要60,000美元, $(options2select).prop('selected',true)所花费的时间可能太长,大约需要50秒!

Note: I used the attr() method before, but read somewhere that prop() is faster, and it's not! 注意:我以前使用过attr()方法,但是在某个地方读到了prop()更快,但事实并非如此!

I'm searching for a way to optimize this task but I can't find any. 我在寻找一种方法来优化这个任务,但我无法找到任何。 Any advice will be appreciated. 任何建议将被认真考虑。

60,000 options in select lists in a browser is not going to be workable on several levels, not least user experience. 浏览器的select列表中的60,000个选项将无法在多个级别上使用,尤其是在用户体验上。 It's just always going to be slow and awkward, not to mention incomprehensible to the user. 它总是很慢而且很尴尬,更不用说让用户难以理解了。 So the best thing to do here is to not do that, to put things into categories, etc., etc. 因此,这里要做的最好的事情就是不要那样做,把事情归类,等等。

Answering the actual question, though: 回答实际问题:

  1. Assuming options2select is an array (since you haven't said what it is), this would probably be faster: 假设options2select是一个数组(因为您没有说它是什么),这可能会更快:

     options2select.forEach(function(option) { option.selected = true; }); 

    or even 甚至

     var n; for (n = options2select.length - 1; n >= 0; --n) { options2select[n].selected = true; } 
  2. Assuming options2select is a selector, much the same with document.querySelectorAll at the outset: 假设options2select是一个选择器,一开始与document.querySelectorAll相同:

     var list = document.querySelectorAll(options2select); Array.prototype.forEach.call(list, function(option) { option.selected = true; }); 

    or even 甚至

     var list = document.querySelectorAll(options2select); var n; for (n = list.length - 1; n >= 0; --n) { list[n].selected = true; } 

But again, faster doesn't mean fast . 但同样, 更快并不意味着很快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM