如何使用jQuery属性选择器选择多个元素 [英] How do I select more than one element using the jQuery attribute selector

查看:300
本文介绍了如何使用jQuery属性选择器选择多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要选择两个元素

<input id="iMe" /> and <span id="sMe">Blah</span>

我想同时选择它们:

$("span[id$='Me']") and $("input[id$='Me']")

在一个选择器中.我尝试过:

in one selector. I've tried:

$("span,input[id$='Me']") -> Nope
$("span[id$='Me'],input[id$='Me']") -> Nope
$("span[id$='Me']input[id$='Me']") -> Nope

我也不介意将其添加到集合中.我绝对不想创建更多脚本来解决这个问题.有什么想法吗?

I wouldn't mind just adding it to the collection either. I definitely don't want to create more script to hack around this. Any ideas?

推荐答案

示例页面: http://jsbin.com/idali (在 http://jsbin.com/idali/edit 上查看/编辑源代码) )

Example page: http://jsbin.com/idali (view/edit source at http://jsbin.com/idali/edit)

以下变体都应该起作用:

The following variations should all work:

$("span[id$=Me], input[id$=Me]")
$('span[id$=Me], input[id$=Me]')
$("span[id$='Me'], input[id$='Me']")
$('span[id$="Me"], input[id$="Me"]')

(下面的原始答案是错误的;引号是可选的,但允许...)

(edit: the original answer below is wrong; quotes are optional but allowed...)

尝试中的引用过多...您想要

You've got too much quoting in your attempts... You want

$("span[id$=Me],input[id$=Me]")

css选择器中未引用属性值.

Attribute values are not quoted in css selectors.

这篇关于如何使用jQuery属性选择器选择多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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