1

我试图避免以下情况:

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() 设置为两者相同,以避免这种重复?

4

2 回答 2

3

尝试使用.add见下文,

dynS.add(cssA).empty().text('#target p:first-of-type {color: red;}');
于 2012-05-09T22:28:54.320 回答
1

采用.add

dynS.add(cssA).empty().text('#target p:first-of-type {color: red;}');
于 2012-05-09T22:28:57.167 回答