javascript jQuery:用一条链定义多个变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8150366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Define multiple variables with a single chain?
提问by Chris Spittles
Is it possible to define multiple variables with a single jQuery chain?
是否可以使用单个 jQuery 链定义多个变量?
var sectionTable = jQuery("#sectionsTable");
var sectionTableRows = sectionTable.find("tr");
var sectionTableColumns = sectionTableRows.find("td");
I don't know what the syntax would be if it is possible but if you took the 3 variables above how could you chain them and would it be considered good practice?
如果可能的话,我不知道语法是什么,但是如果你采用了上面的 3 个变量,你怎么能把它们链接起来,这会被认为是好的做法吗?
Many thanks
非常感谢
Chris
克里斯
EDIT: Wow, thanks for all the comments. Sorry for being vague, what I was after was a better way (if one exists) of defining child variables from a parent variable. That's why I thought of using the chain and wondered if a way existed. Thanks for the great advice.
编辑:哇,感谢所有评论。抱歉我含糊其辞,我所追求的是一种从父变量定义子变量的更好方法(如果存在的话)。这就是为什么我想到使用链条并想知道是否存在一种方法。感谢您的宝贵意见。
回答by jAndy
I maybe don't really understand what you want or need, but you can chain any function for a jQuery wrapped set and "end" it and therefor "go back" to the previous set. For instance:
我可能不太明白您想要或需要什么,但是您可以为 jQuery 包装集链接任何函数并“结束”它,然后“返回”到前一个集。例如:
jQuery("#sectionsTable").css('background-color', 'red')
.find('tr').css('background-color', 'yellow')
.find('td').css('background-color', 'black')
.end() // back to 'tr'
.doSomething()
.end() // back to '#sectionsTable'
.doSomething();
However, this would imply that you only need to access those elements once. If you need to access them later in your code, you alwaysshould store the results references in variables for several performance reasons.
但是,这意味着您只需访问这些元素一次。如果您稍后需要在代码中访问它们,出于多种性能原因,您应该始终将结果引用存储在变量中。
回答by gion_13
If you really want to, anything is possible :
如果你真的想,一切皆有可能:
Of the top of my head, you could try to do something like this :
在我的头顶,你可以尝试做这样的事情:
var sectionTable,
sectionTableRows,
sectionTableColumns = $('td', (sectionTableRows = $('tr',(sectionTable = $('#sectionsTable')))));
Another ideea would be to build a mini-plugin that assigns the current jquery object to a certain field of an object :
另一个想法是构建一个迷你插件,将当前的 jquery 对象分配给对象的某个字段:
jQuery.fn.assignTo = function(variableName,namespace){
var ns = namespace || window;
ns[variableName] = this;
return this;
}
With this peace of code you could do the following :
有了这种代码的和平,您可以执行以下操作:
var sections = {};
jQuery("#sectionsTable")
.assignTo('sectionTable',sections)
.find("tr")
.assignTo('sectionTableRows',sections)
.find("td")
.assignTo('sectionTableColumns',sections);
console.log(sections.sectionTable);
console.log(sections.sectionTableRows);
console.log(sections.sectionTableColumns);
Of course, if you do not specify any namespace, the variables will be global (will be attached to the window object);
当然,如果不指定任何命名空间,变量将是全局的(将附加到窗口对象);
Any way, I do not encourage you to use these examples, because it doesn't make very much sense to worsen your code's readability in favour of fewer equal signs and var
declarations.
无论如何,我不鼓励您使用这些示例,因为为了减少等号和var
声明而降低代码的可读性并没有多大意义。
回答by Mathias Bynens
You could rewrite it as follows:
您可以按如下方式重写它:
var $sectionTable = $('#sectionsTable'),
$sectionTableRows = $('tr', $sectionTable),
$sectionTableColumns = $('td', $sectionTableRows);
But of course, that's only useful if you're actually gonna use all three of those variables.
但是,当然,这只有在您实际上要使用所有这三个变量时才有用。
回答by Saeed Neamati
The only thing comes to my mind is one-var declaration, like:
我唯一想到的是one-var 声明,例如:
var sectionTable = jQuery("#sectionsTable"),
sectionTableRows = sectionTable.find("tr"),
sectionTableColumns = sectionTableRows.find("td");
回答by Matt
Why would you want to?
你为什么要?
What's wrong with defining the three variables?
定义三个变量有什么问题?
(No, you can't).
(不,你不能)。
If you didn't needthe sectionTable
or sectionTableRows
variables at all, you could of course do;
如果您没有需要的sectionTable
或sectionTableRows
变量都,你当然可以做;
var sectionTableColumns = jQuery("#sectionsTable").find("tr").find("td");
Which, using the descendant selector, could be shortened to just;
其中,使用后代选择器,可以缩短为;
var sectionTableColumns = jQuery("#sectionsTable tr td");