如何使 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>

如何在html动态中制作" href"属性?

好吧,您的变量具有所需的值…

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

…所以请使用它:

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

最新更新