我想写一个优雅的解决方案,通过搜索不同的上下文列表来选择相同的标签
//i have this not editable code
var c1$ = $('#context1'),
c2$ = $('#context2'),
c3$ = $('#context3');
//I want to improve the code and 'only from here on
var selector = 'a.select';
c1$.find(selector).css('color','red');
c3$.find(selector).css('color','red');
Run Code Online (Sandbox Code Playgroud)
我正在寻找任意数量的上下文的解决方案
您可以使用它$.add()来构建上下文,然后在选择器的context参数中将其用作单个jQuery对象:
var $foo = $("#foo"),
$bar = $("#bar");
var $context = $foo.add($bar);
$("a.select", $context).css("color", "red");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1235 次 |
| 最近记录: |