2014-12-28 27 views
0

影響我有這樣的代碼:GWT - setProperty方法不能在FireFox瀏覽器

 colImage.getElement().getStyle().setProperty(isRtl ? "margin-right" : "margin-left", "41px"); 
     colImage.getElement().getStyle().setProperty(isRtl ? "margin-left" : "margin-right", "5px"); 

在Chrome瀏覽器中正常工作,並使得相應41px和5px的保證金。
但在FireFox瀏覽器中,它會忽略這些命令,並且不會放入任何margin屬性。

而且還沒有完成。
我對另一個成員有相同的命令(稱爲rowImage)。在那裏,FireFox瀏覽器使邊距爲41px,但沒有留下餘量。

有人可以給我一個這種行爲的解釋嗎?
我有

setProperty("pointer-events", "auto"); 

火狐完全忽略了同樣的經歷。

回答

1

不幸的是,根據事物的JavaScript端,有沒有稱爲pointer-events的CSS樣式屬性,甚至沒有margin-left,background-color等。相反,必須使用camel-ca讓所有這些屬性使它們按預期生效。

嘗試這些替代:

colImage.getElement().getStyle().setProperty(isRtl ? "marginRight" : "marginLeft", "41px"); 
colImage.getElement().getStyle().setProperty(isRtl ? "marginLeft" : "marginRight", "5px"); 

... 

setProperty("pointerEvents", "auto"); 

事實上,的setProperty應該與那些樣式屬性的dash'd版本失敗。從https://gwt.googlesource.com/gwt/+/master/user/src/com/google/gwt/dom/client/Style.java#2111

/** 
* Sets the value of a named property in the specified units. 
*/ 
public final void setProperty(String name, double value, Unit unit) { 
    assertCamelCase(name); 
    setPropertyImpl(name, value + unit.getType()); 
} 

... 

/** 
* Assert that the specified property does not contain a hyphen. 
* 
* @param name the property name 
*/ 
private void assertCamelCase(String name) { 
    assert !name.contains("-") : "The style name '" + name 
     + "' should be in camelCase format"; 
} 

在開發模式或超級開發模式下運行會運行這些斷言(或斷言編譯啓用,儘管這可以是一個痛苦),它就會失敗,當你試圖調試,用建設性的錯誤信息。

+0

謝謝,你說得對。但我不明白爲什麼在'FireBug'手動設置樣式屬性'margin-left'時,它的行爲如我所料 – Aharon

+1

當你使用螢火蟲時,你是在操縱CSS本身,而不是通過JS API。 GWT是Java編譯爲JS的,所以只能使用JS API。 –

0

我認爲使用getElement().getStyle().setProperty()方法是一個壞主意。試着在你的CSS .is_rtl.not_is_rtl樣式定義:

.is_rtl {  
    margin-left : 5px !important; 
    margin-right : 41px !important; 
} 

.not_is_rtl { 
    margin-left : 41px !important; 
    margin-right : 5px !important; 
} 

在你的代碼將:

private static final String IS_RTL_STYLE = "is_rtl"; 
private static final String NOT_IS_RTL_STYLE = "not_is_rtl"; 
.... 
colImage.addStyleName(isRtl ? IS_RTL_STYLE : NOT_IS_RTL_STYLE); 
相關問題