同时使用2个变量
我该怎么做呢?
$(function() {
var foo = $('#foo'),
bar = $('#bar');
$('body').click(function() {
$(foo,bar).css({color: 'red'});
});
});
演示:http://jsfiddle.net/each/RGZ4Z/ - 只有 foo 变成红色
编辑:我可以强调这样一个事实:我知道我可以轻松做到:
$('#foo,#bar').css({color: 'red'});
我只是询问变量的用法......
How do I go about doing this?
$(function() {
var foo = $('#foo'),
bar = $('#bar');
$('body').click(function() {
$(foo,bar).css({color: 'red'});
});
});
Demo: http://jsfiddle.net/each/RGZ4Z/ - Only foo becomes red
Edit: Can i stress the fact that I'm aware I could easily do:
$('#foo,#bar').css({color: 'red'});
I'm simply asking about the usage of variables...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许,您想要的是这样的:
这是一段演示代码,展示了它是如何工作的: http://jsfiddle .net/jfriend00/3Ep6J/。
根据您的需要,我可以想到三种选择:
var items = $('#foo, #bar')
$('#foo, #bar').css( {color: 'red'});
foo.add(bar).css({color: 'red'});
并且,每个选项的更多详细信息:
1) 您可以要么在一开始就创建一个 jQuery 对象,其中包含两组对象像这样:
然后再这样做:
2)或者一次完成所有操作:
3)或者,如果您已经有单独的 foo 和 bar 的 jQuery 对象,您可以将一个 jQuery 对象中的项目添加到另一个 jQuery 对象中,然后执行您的操作:
然后执行以下操作:
注意:有点违反直觉,选项 3) 不会修改
foo
jQuery 对象,add
方法返回一个新的 jQuery 对象与来自的物品添加了bar
。Probably, what you want is this:
Here's a demo piece of code that shows how this can work: http://jsfiddle.net/jfriend00/3Ep6J/.
There are three choices I can think of depending upon what you want:
var items = $('#foo, #bar')
$('#foo, #bar').css({color: 'red'});
foo.add(bar).css({color: 'red'});
And, more detail on each option:
1) You can either just create one jQuery object in the beginning that has both sets of objects in it like this:
and then later do this:
2) Or just do it all at once:
3) Or, if you already have separate jQuery objects for foo and bar, you can add the items from one jQuery object to the other and then carry out your operation:
and then later, do this:
Note: somewhat counterintuively, option 3) does not modify the
foo
jQuery object, theadd
method returns a new jQuery object with the items frombar
added to it.+1 jfriend00 的答案,但我想我应该插话解释为什么你的答案首先不起作用。
jQuery“构造函数” 具有三个方法签名(本质上):
$(selector[, context ])
用于从现有元素中创建 jQuery 对象$(html[, OwnerDocument])
用于创建新元素$(callback)
作为 的别名$(document).ready(callback)
您正在使用第一个选项,并且
selector
是一个预先存在的 jQuery 对象(这很好),但是通过添加bar
参数,这意味着它将该元素视为上下文。本质上:“给我一个结果集,其中包含元素bar
中存在的所有元素foo
”。另外,如果您知道变量中已经有一个 jQuery 对象,那么再次调用
$()
就太浪费了。为了帮助我记住,我总是在 jQuery 变量前加上 $ 前缀:
+1 to jfriend00's answer, but I thought I'd chime in to explain why yours wasn't working in the first place.
The jQuery "constructor" has three method signatures (essentially):
$(selector[, context])
for creating a jQuery object out of existing elements$(html[, ownerDocument])
for creating new elements$(callback)
as an alias for$(document).ready(callback)
You're using the first option, and
selector
is a pre-existing jQuery object (which is fine), however by adding thebar
argument, that means that it is treating that element as the context. Essentially: "give me a result set containing all the elementsfoo
which exist inside the elementbar
".Also, if you know that you already have a jQuery object in a variable, it's just wasteful to wrap it another call to
$()
.To help me remember, I always prefix jQuery variables with $: