jQuery多个选择器,但缓存在一个变量中



我试图避免以下情况:

var dynS = $('#dyn-style');
var cssA = $('#css-active');
$('#selectors li').click(function(){
    switch($(this).attr('data-id')){
        case 'first-of-type':
            dynS.empty().text('#target p:first-of-type {color: red;}');
            cssA.empty().text('#target p:first-of-type {color: red;}');

有什么方法可以使用缓存的选择器变量并将 text() 设置为相同,以避免这种重复?

尝试使用.add见下文,

dynS.add(cssA).empty().text('#target p:first-of-type {color: red;}');

使用.add

dynS.add(cssA).empty().text('#target p:first-of-type {color: red;}');

最新更新