zoukankan      html  css  js  c++  java
  • window.location.href用法与a标签的比较

    1.在使用这两种方法进行页面的跳转时,这两种方法都能够有效的实现该功能

    但是其原理不尽相同

    第一:window.location.href()方法必须书写在js中

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <a  onclick="submit1()"></a>
    <a  onclick="submit2()"></a> </body> <script> function submit1(){ // window.alert('测试'); window.location.href="/https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=%E7%99%BE%E5%BA%A6"; } function submit2(){ // window.alert('测试'); window.location.href="/url"; } </script> </html>

     该方法主要通过给a标签添加点击事件跳转页面;

    第二种

    <a href="https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=%E7%99%BE%E5%BA%A6"></a>

    该方法直接进行跳转不需要任何中间步骤,

    因此如果只是想进行网页的跳转建议使用a标签直接跳转,方便简洁;

    除此之外如果是想进行与java后台相结合提供参数的话建议使用window.location.href方法

    jsp文件:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <input type="button" value="提交数据1" onclick="submit1()">
    <input type="button" value="提交数据2" onclick="submit2()">
    </body>
    <script>
        function submit1(){
            // window.alert('测试');
        window.location.href="bb?method=add";
        }
        function submit2(){
            // window.alert('测试');
            window.location.href="bb?method=update";
        }
    </script>
    </html>
    

     sevlet文件:

    package com.ztjy.smsw.servlet;
    
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class jsSubmit extends HttpServlet {
        @Override
    
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        获取j携带参数
            String key=req.getParameter("method");
                if (key.equals("add")){
                    System.out.println("去执行add方法");
                }
                if (key.equals("update")){
                    System.out.println("去完成update修改功能");
                }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req, resp);
        }
    }
    

     希望该原创文章对你有帮助,谢谢。

  • 相关阅读:
    计算机专业及软件开发推荐书籍
    摘抄Interrupt Architecture in Microsoft Windows CE .NET
    摘抄Multithreaded Programming on the Pocket PC with Visual C++
    摘抄Driver Code Structure
    摘抄Multimedia Streaming on Microsoft Windows CE 3.0
    摘抄Creating a Board Support Package Using the Windows CE .NET CEC Editor
    摘抄 Board Support Package, Boot Loader, and Kernel Startup Sequence
    摘抄The Case of the Missing Ordinal
    摘录Windows CE API机制初探
    摘抄非技术追梦—SQUARE大传
  • 原文地址:https://www.cnblogs.com/liwids-blog/p/11943180.html
Copyright © 2011-2022 走看看