如何在HTML动态中制作'href'属性?

时间:2017-04-19 06:03:37

标签: javascript html jsp

说明:

  1. 我在JSP中有一个包含单词的'java.util.list'。

  2. 我在JSP中有另一个'java.util.list',它包含对应的URL链接 每个字。

  3. 我需要在同一个JSP页面上显示这些单词,点击后,他们应该打开相应的链接。

  4. 以下是我的代码尝试:

    <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>
    

1 个答案:

答案 0 :(得分:0)

  

如何制作&#39; href&#39; HTML动态属性?

好吧,你有一个你想要的变量......

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

...所以只需使用它:

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