1

CSS のいくつかの場所に、次のようなエントリがあります。

button {
  background-image: url('../images/button.png');
}

images/すべてのbackground-imageURLのすべての出現を次のようなものに変更する jQuery の関数が必要です。

button {
  background-image: url('../images/Customer/button.png');
}

私は現在、この恐ろしいコードでそれを行っていますが、jQuery を使用するもっときちんとした方法があると思います。イメージの実際の場所は に保持されていることに注意してくださいsessionStorage

// Hacks all CSS styles that refer to the images folder.
for ( s = 0; s < document.styleSheets.length; s++ ) {
  var mysheet=document.styleSheets[s];
  var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules;
  // Look for: background-image: url(images/button.png); 
  for (i=0; i < myrules.length; i++){
    var css = myrules[i].style.cssText;
    if(css.indexOf('images/') != -1 ){
      // Surgery on the cssText because the individual entries are read only.
      // Replace the css.
      myrules[i].style.cssText = css.replace("images/", sessionStorage.images);
      // That's the new style.

    }
  }
}
4

2 に答える 2

0

このプラグインをjQueryで使用します。

jQuery.Rule - http: //flesler-plugins.googlecode.com/files/jquery.rule-1.0.2-min.js

次に、次の手順を実行します。

$(function(){

    var customer = 'Oscar';

    // Get all stylesheets
    $.rule().sheets.map(function(index, cssSheet){
            // Get all css rules from all the stylesheets
        $.each(cssSheet.cssRules, function(index, cssRule){
            // If cssRule is 'background-image' type
            if(cssRule.cssText.indexOf('background-image') != -1){
                // Prepare new cssRule adding the customer
                var newCssRule = cssRule.cssText.replace('/images/', '/images/' + customer + '/');
                // Add new cssRule to the correct stylesheet
                $.rule(newCssRule).appendTo('style');
            }
        });
    });

});

知っておくと、これでもっと多くのことができます。このリンクを見てください:

http://flesler.webs.com/jQuery.Rule/

お役に立てれば :-)

于 2012-08-09T15:09:46.610 に答える
0

ここではjQueryは必要ありません。

の新しいCSS定義を導入しbuttonて、ページに追加してみてください。これにより、以前の定義が上書きされます。

var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = 'button { background-image: url("../images/Customer/button.png"); }';
document.head.appendChild(style);

または、button要素のインラインCSSを調整します。

var buttons = document.getElementsByTagName('button'),
    i = 0;

for (; i < buttons.length; i++) {
    buttons[i].style.backgroundImage = 'url("../images/Customer/button.png")';
}
于 2012-08-09T15:01:57.103 に答える