在jQuery中有样式选择器吗? [英] Is there a style selector in jQuery?

查看:135
本文介绍了在jQuery中有样式选择器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想选择其中的所有图片,例如Home,可以这样做:

  $ (img [alt ='Home'])

但是如何选择

编辑:没有这样的选择器,有没有任何插件,或任何计划,在下一个jQuery版本?

解决方案

一个好主意,但你可以为它添加一个新的Sizzle选择器:

  $。expr [':']。width = function(elem,pos,match){
return $(elem).width()== parseInt(match [3]);
}

您可以这样使用:

  $('div:width(970)')


b $ b

这样会变得非常缓慢,因此你可以缩小要比较的元素数量,例如:

  $('#navbar> div:width(970)')

仅选择作为导航条直接后代的div,其宽度也为970像素。


If I want to select every image which it's alt is Home for example, I can do something like this:

$("img[alt='Home']")

But how can I select every elements which their width CSS property is 750px for example in a single selector?

EDIT: If there is no such selector, is there any plugin, or any plans to do it in the next jQuery versions?

解决方案

Not necessarily a great idea, but you could add a new Sizzle selector for it :

$.expr[':'].width = function(elem, pos, match) {
    return $(elem).width() == parseInt(match[3]);
}

which you could then use like so:

$('div:width(970)')

That's going to be horrifically slow, though, so you'd want to narrow down on the number of elements you're comparing with something like :

$('#navbar>div:width(970)')

to only select those divs that are direct descendants of the navbar, which also have a width of 970px.

这篇关于在jQuery中有样式选择器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆