如何使用 jquery get select 元素具有 MULTIPLE 模式和反向
我今天的工作有一堆。 我的堆栈在这里:
我有一个选择html元素,它有多重模式:(
<select class="test" MULTIPLE></select>
多重模式也输入为:multiple =“multiple”我在这里包括)
<select class="test" multiple='multiple'></select>
现在我只想在许多正常选择元素中选择这个元素:
<select class="test" ></select>
<select class="test" ></select>
<select class="test" multiple='multiple'></select>
<select class="test"> </select>
我使用jQ就像this:
$(".test[!MULTIPLE]").css('border','solid 1px red');
但是所有选择元素都有红色边框;
我怎样才能只选择多个元素。并且选择不是“多重”模式?
I have a stack with my work today.
My stack here:
I have a select html element and it has MULTIPLE mode:
<select class="test" MULTIPLE></select>
(MULTIPLE mode also type as : multiple="multiple" i inclue here)
<select class="test" multiple='multiple'></select>
now i only want select this element in many normal select element:
<select class="test" ></select>
<select class="test" ></select>
<select class="test" multiple='multiple'></select>
<select class="test"> </select>
i was using jQ like this:
$(".test[!MULTIPLE]").css('border','solid 1px red');
but all select element has border red;
How can i get only select element MULTIPLE. And get select not MULTIPLE mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
编辑: 正如 Reigel 提到的,如果避免使用 jQuery 伪选择器,您可以获得相同的结果集和更好的性能:
编辑 2: 正如您可以从评论中,一些快速测试表明,至少对于我们中的一些人来说,第二个选项实际上更慢。进一步挖掘,根据 css3.info,对
的本机支持:not
CSS3 选择器比我想象的更广泛。假设 jQuery 在可用时使用本机选择器,这可能会造成所有差异(对不起 IE7)。编辑3:进一步感谢@nickf,他在IE8中运行了这些测试,发现两者之间没有实质性差异。鉴于所有这些歧义,明智的做法是在目标浏览器中测试 jQuery 伪选择器(特别是
:not
/.not
)是否陷入代码热点对性能有重大影响的点(如果您有受控的目标浏览器环境)。但如果您的目标是所有浏览器,那么最好的建议似乎是使用最适合您的代码的内容,并避免过早优化。
Try this:
Edit: As Reigel mentions, you can get the same result set with better performance if you avoid the jQuery pseudo-selector:
Edit 2: As you can tell from the comments, some quick testing shows that, at least for a few of us, the second option is actually slower. Digging further, according to css3.info, native support for the
:not
CSS3 selector is more widespread than I thought. That probably makes all the difference (sorry IE7), assuming jQuery uses the native selector when available.Edit 3: Further thanks to @nickf, who ran these tests in IE8, and found no substantive difference between the two. In light of all this ambiguity, it would be wise to test in your target browser if jQuery pseudo-selectors, or
:not
/.not
specifically, fall into a code hot spot that has a material impact on performance (and if you have a controlled target browser environment).But if your target is all browsers, it looks like the best advice is to use what best fits your code, and avoid premature optimization.
但如果您使用 IE 7 及更低版本,选择元素中的边框不会按预期更改。
but if you are using IE 7 and below, borders in select elements won't change as expected..
另一种保持整个页面通用而不局限于类的方法是这样的 -
JSFiddle 链接
Another way to keep it generic across the page without being restricted to class is this way -
JSFiddle link