簡體   English   中英

當javascript返回條件時如何在JSTL中使用if-else選項

[英]How to use if-else option in JSTL when the condition returned by javascript

當條件是JavaScript代碼返回的變量時,如何在JSTL中使用if else

當媒體是Tablet或ipad時,我需要添加一些Tag HTML,否則,則需要添加其他Tag Html。

 <c:choose>
   <c:when test="${TabletMedia}">
      // Tags HTML
   </c:when>

   <c:otherwise>
     // other Tags HTML
   </c:otherwise>
  </c:choose>

function TabletteMedia() {

    if (navigator.userAgent.match(/(android|iphone|ipad|blackberry|symbian|symbianos|symbos|netfront|model-orange|javaplatform|iemobile|windows phone|samsung|htc|opera mobile|opera mobi|opera mini|presto|huawei|blazer|bolt|doris|fennec|gobrowser|iris|maemo browser|mib|cldc|minimo|semc-browser|skyfire|teashark|teleca|uzard|uzardweb|meego|nokia|bb10|playbook)/gi))
    {
        if (((screen.width >= 480) && (screen.height >= 800)) || ((screen.width >= 800) && (screen.height >= 480)) || navigator.userAgent.match(/ipad/gi))
        {
            return true;
        }
    }
    return false;

}

TabletMedia = TabletteMedia();

你不可以做這個。 JSP頁面與JSTL一起被編譯到一個類中,與運行任何JavaScript無關。 JSTL在服務器端運行,當它決定將哪種靜態內容(腳本和HTML)返回給客戶端時。 您可以使用JSTL來調節添加到JavaScript或HTML中的內容,但是您不能從中調用JavaScript,因為腳本是在客戶端(即瀏覽器)中運行的,當頁面被加載時,當所有靜態內容都已返回時由服務器。

如果您的條件僅在頁面加載時可用,則必須使用JavaScript而不是JSTL標簽來更改頁面結構。

你不能

因為

欲了解更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM