2012-11-16 41 views
1

在我的標準網頁中,我有以下代碼,但是如何在XPage中執行此操作?如何在XPage中動態設置HTML標記

<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ --> 
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]--> 
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]--> 
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]--> 
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]--> 

回答

1

如果你能逃脫把類的<身體>標籤,你可以通過一個主題做集中,所以沒有必要重複每一頁上的代碼:

<control> 
     <name>ViewRoot</name> 
     <property> 
      <name>styleClass</name> 
      <value>#{javascript:if (context.getUserAgent().isIE(0,6)) { 
    return 'no-js lt-ie9 lt-ie8 lt-ie7'; 
} else if (context.getUserAgent().isIE(7,7)) { 
    return 'no-js lt-ie9 lt-ie8'; 
} else if (context.getUserAgent().isIE(8,8)) { 
    return 'no-js lt-ie9'; 
}}</value> 
     </property> 
    </control> 
0

您可以檢查哪些鮑澤,通過本網頁http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Browser_compatibility

代碼如果然後其他人就可以在計算文本,快速的方式使用。並在if語句中返回正確的html。 您需要設置計算文本到HTML

+0

示例HTML代碼是關於設置頂級標記,我不認爲你可以用計算文本來做到這一點。 –

+0

thimop,我同意了。我讀得不好。這是一個高於普通標籤的水平。 –

6

您可以通過添加屬性到UIViewRoot組件的做到這一點:

<?xml version="1.0" encoding="UTF-8"?> 
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"> 

    <xp:this.attrs> 

     <!-- if browser is IE 7 or smaller --> 
     <xp:attr name="class" value="no-js lt-ie9 lt-ie8 lt-ie7" 
     rendered="#{javascript:context.getUserAgent().isIE(0,7)}"> 
     </xp:attr> 

     <!-- if browser is NOT IE 7 or smaller --> 
     <xp:attr name="class" value="no-js lt-ie9 lt-ie8 lt-ie7" 
     rendered="#{javascript:!(context.getUserAgent().isIE(0,7))}"> 
     </xp:attr> 

    </xp:this.attrs> 

</xp:view> 

屬性由XPage中的當前語言設置來計算。 要做這個應用程序/服務器範圍廣,你必須添加到主題。

編輯:

的主題必須是這樣的:需要

<control> 
    <name>ViewRoot</name> 
     <property> 
     <name>attrs</name> 
      <complex type="xp_attr"> 
       <property> 
        <name>name</name> 
        <value>class</value> 
       </property> 
       <property> 
        <name>value</name> 
        <value>#{javascript: 
        if (context.getUserAgent().isIE(0,6)) { 
         return 'no-js lt-ie9 lt-ie8 lt-ie7'; 
        } 
        if (context.getUserAgent().isIE(7,7)) { 
         return 'no-js lt-ie9 lt-ie8'; 
        } 
        if (context.getUserAgent().isIE(8,8)) { 
         return 'no-js lt-ie9'; 
        } 
        return ''; 
        }</value> 
      </property> 
     </complex> 
     </property> 
</control> 

空的回報,否則主題將失敗。

+0

非常聰明。我試圖用一個主題做到這一點,但只能得到標籤的類 –

+1

添加主題版本到我的答案。 –

+0

我更喜歡帶有計算值屬性的單個attr。在這個特定的情況下,?:操作符級聯可以做到這一點。 –

相關問題