2016-10-09 6 views
0

これに関してはSOにはほとんど解決策はありませんが、私の状況には関係ありません。実行時にGWTのCSSテーマを変更するには?

warフォルダに別のCSSファイルがあります。

W3-テーマblue.css私の戦争のフォルダに

私の戦争フォルダに

W3-テーマred.css。

私は青のテーマをロードしたい場合は今、私は私のproject.htmlファイルでこれを書く

 <link rel="stylesheet" href="/w3-theme-blue.css"> 

私は赤のテーマをロードする場合、私は私のproject.htmlファイル

 <link rel="stylesheet" href="/w3-theme-red.css"> 
でこれを書きます

ここで達成したいのは、実行時に青から赤に、またはその逆に変更することです。

私はJSNIでこれを使用

http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

JavaScriptでこれを確認したが、運

その後、私はこの

 Element s = DOM.createElement("link"); 
    s.setAttribute("type", "text/css"); 
    s.setAttribute("href", "w3-theme-red.css"); 
    DOM.getElementById("body").appendChild(s); 
のように、GWTで同じコードを実装しようとしません

しかし、まだ運がない...

Pl他のことができて、あなたのスタイルを置き換えるには

// safe your StyleElement that is returned with injectStyleSheet() 
// so you can override it later with a new style 
public static StyleElement content; 

[...] 

CssResource style = GWT.create(CssBundle.class); 
content = StyleInjector.injectStylesheet(style.css1().getText()); 

:あなたはJSNI

private native void loadCSS (String cssHref) /*-{ 
    $doc.write('<link rel="stylesheet" type="text/css" href="' + 
cssHref + '">'); 
}-*/ 

答えて

0

をガイドを和らげますこれを行う:

// overrides the content, that you defined before with your second theme 
    StyleInjector.setContents(content, style.css2().getText()); 

    // setContents docu says that it might not work as expected in IE... 
    // maybe you have to use inject() then and delete the old style, 
    // otherwise you are adding more and more styles after each theme change 

    // StyleInjector.inject(style.css2().getText()); 
私は、これはあなたが行くの取得を望む

...

+0

おかげで、それは document.wrtieは機能、応答を編集した任意の提案 – junaidp

+0

... GWT JSNIの構文を確認してくださいではないと言います –

0

使用StyleInjectorを使用し、その後、動的にスタイルシートを読み込む必要がある場合は...

おかげ

関連する問題