2017-04-19 61 views
-1

說明:如何使HTML動態中的'href'屬性?

  1. 我在JSP中的 'java.util.List的' 包含的話。

  2. 我在JSP中有另一個'java.util.list',它包含對應於每個詞 的URL鏈接。

  3. 我需要在同一個JSP頁面上顯示這些單詞,點擊時應打開它們的相應鏈接。

以下是我的代碼的嘗試:

<html> 
    <head> 
     <style> 
      .container{color:blue;} 
      #text{width:50%;} 
     </style> 

    </head> 
    <body> 
     <div class="container"> 
      <form action="" method="get" > 
      <label>Node Path:</label> 
      <input id="text" type="text" name ="path" /> 
      <input type= "submit" value="submit" /> 
      </form> 
     </div> 
     <div> 
      <br> 
      <br> 
      <%! String absolutePath; %> 

      <%  
       com.intel.package1.HelloService service = sling.getService(      com.intel.package1.HelloService.class); 

       String path = request.getParameter("path") ; 

       if (path != null){ 
        java.util.List nodeNames = service.getNodeNames(path); 
        java.util.List nodePaths= service.getNodePaths(path); 
        java.util.List nodeDepths = service.getNodeDepths(path); 

        for (int i=0;i<nodeNames.size();i++){ 
         int depth = (int)nodeDepths.get(i); 
         for(int j=0;j<depth;j++){ 
          out.println("&nbsp&nbsp&nbsp&nbsp&nbsp"); 
         } 
         absolutePath= "http://localhost:4502/crx/de/index.jsp#"+nodePaths.get(i); 
         String start = "<a href='' onclick='fns()'>"; 
         out.println(start+nodeNames.get(i)+"</a>" ); 
         out.println("<br>"); 

         // for(int j=0;j<depth;j++){ 
         // out.println("&nbsp&nbsp&nbsp&nbsp&nbsp"); 
         // } 
         // out.println(absolutePath); 
         out.println("<br>"); 
         // out.println("<br>"); 
       } 
      } 
      %> 
     </div> 
     <script> 
      function fns(){ 

       var value= "<%=absolutePath%>"; 
       //alert('hello'); 

       window.open(value, '_blank'); 
      } 
     </script> 
    </body> 
</html> 
+0

您是否嘗試過的東西?您可以在此處發佈以獲取解決方案。 – Jai

+0

我想保持簡潔。但確定會發佈它。 –

+0

@SameerSah下次嘗試正確縮進代碼(我在編輯器中快速執行,因此不完美),這將更加可讀,關於10空行的相同的事情是不必要的。 – AxelH

回答

0

如何使HTML中的 'HREF' 屬性的動態?

好了,你有你想要的值的變量...

absolutePath= "http://localhost:4502/crx/de/index.jsp#"+nodePaths.get(i); 
String start = "<a href='' onclick='fns()'>"; 

...所以只是用它:

String start = "<a href='" + absolutePath +"'>"; 
+0

這工作!非常感謝你。 :) –