重构:Javascript 块

发布于 2024-10-11 03:56:53 字数 871 浏览 2 评论 0原文

需要一些帮助来重构此代码:

$("span[rel=color_content]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_content]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_link]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_link]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_selected]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_selected]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_page]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_page]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_player]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_player]").css('background-color', '#' + hex);
  }
});

每个代码之间唯一发生变化的是 rel 属性的内容。

Need some help refactoring this code:

$("span[rel=color_content]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_content]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_link]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_link]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_selected]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_selected]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_page]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_page]").css('background-color', '#' + hex);
  }
});
$("span[rel=color_player]").ColorPicker({
  onChange: function (hsb, hex, rg) {
    $("span[rel=color_player]").css('background-color', '#' + hex);
  }
});

The only thing changing between each is the contents of the rel attribute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一指流沙 2024-10-18 03:56:53
// Newlines added for readability
$('span[rel=color_content],
   span[rel=color_link],
   span[rel=color_selected],
   span[rel=color_page],
   span[rel=color_player]').ColorPicker({
       onChange: function(hsb, hex, rg){
           $(this).css('background-color', '#' + hex);
       }
   });

我猜页面上每个跨度只有一个实例。如果没有,您可以稍微修改一下以获得行为相同的方式:

// Newlines added for readability
$('span[rel=color_content],
   span[rel=color_link],
   span[rel=color_selected],
   span[rel=color_page],
   span[rel=color_player]').ColorPicker({
       onChange: function(hsb, hex, rg){
           $('span[rel='+$(this).attr('rel')+']')
               .css('background-color', '#' + hex);
       }
   });
// Newlines added for readability
$('span[rel=color_content],
   span[rel=color_link],
   span[rel=color_selected],
   span[rel=color_page],
   span[rel=color_player]').ColorPicker({
       onChange: function(hsb, hex, rg){
           $(this).css('background-color', '#' + hex);
       }
   });

I'm guessing there's only one instance of each span on the page. If not, you can modify this slightly to get something that behaves the same way:

// Newlines added for readability
$('span[rel=color_content],
   span[rel=color_link],
   span[rel=color_selected],
   span[rel=color_page],
   span[rel=color_player]').ColorPicker({
       onChange: function(hsb, hex, rg){
           $('span[rel='+$(this).attr('rel')+']')
               .css('background-color', '#' + hex);
       }
   });
你的背包 2024-10-18 03:56:53

你可以这样做:

function setColorPicker(css_selector) {
    $(css_selector).ColorPicker({
      onChange: function (hsb, hex, rg) {
         $(css_selector).css('background-color', '#' + hex);
      }
    });
}

You could do something like:

function setColorPicker(css_selector) {
    $(css_selector).ColorPicker({
      onChange: function (hsb, hex, rg) {
         $(css_selector).css('background-color', '#' + hex);
      }
    });
}
看透却不说透 2024-10-18 03:56:53
for(i in ['content','link','selected','page','player'])
{
    $("span[rel=color_"+i+"]").ColorPicker({   
    onChange: function (hsb, hex, rg) {$("span[rel=color_"+i+"]").css('background-color', '#' + hex);   } }); 
}
for(i in ['content','link','selected','page','player'])
{
    $("span[rel=color_"+i+"]").ColorPicker({   
    onChange: function (hsb, hex, rg) {$("span[rel=color_"+i+"]").css('background-color', '#' + hex);   } }); 
}
马蹄踏│碎落叶 2024-10-18 03:56:53

如果您最终需要维护一长串信息,一种方法是将所有变化的信息放入一个易于维护的变量中:

var rels = ['color_content','color_link','color_selected','color_page'];

然后您可以:

// build the selectors according to your patterh
var selectors = $.map( rels, function(e,i){ 
  return 'span[rel=' + rels + ']'; 
}).join(',');

$(selectors).ColorPicker({
  onChange: function(hsb,hex,rg){
    $(this).css('background-color','#'+hex); 
});

If you end up with a really long list of these to maintain, one way is to put all the changing information in one easily-maintainable variable:

var rels = ['color_content','color_link','color_selected','color_page'];

Then you could have:

// build the selectors according to your patterh
var selectors = $.map( rels, function(e,i){ 
  return 'span[rel=' + rels + ']'; 
}).join(',');

$(selectors).ColorPicker({
  onChange: function(hsb,hex,rg){
    $(this).css('background-color','#'+hex); 
});
舂唻埖巳落 2024-10-18 03:56:53
var arr = [
    'color_content',
    'color_link', 
    'color_selected', 
    'color_page', 
    'color_player'
];

function foo() {
    var i, selector;

    for (i = 0; i < arr.length; i++) {
        selector = 'span[rel=' + arr[i] + ']';

        $(selector).ColorPicker({
            onChange: function (hsb, hex, rg) {
                $(selector).css('background-color', '#' + hex);
            }
        });
    }
}
var arr = [
    'color_content',
    'color_link', 
    'color_selected', 
    'color_page', 
    'color_player'
];

function foo() {
    var i, selector;

    for (i = 0; i < arr.length; i++) {
        selector = 'span[rel=' + arr[i] + ']';

        $(selector).ColorPicker({
            onChange: function (hsb, hex, rg) {
                $(selector).css('background-color', '#' + hex);
            }
        });
    }
}
拥抱我好吗 2024-10-18 03:56:53

您可以使用逗号分隔多个 CSS 选择器:

$("span[rel=color_player], span[rel=color_page]").ColorPicker();

You can separate several CSS selectors using a comma:

$("span[rel=color_player], span[rel=color_page]").ColorPicker();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文