点击跳转页面并执行跳转页面中的某个函数

1、新建html文件


<html lang="en">
head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js">script>
    <title>Documenttitle>
head>
<body>
    <a href="test.html" id="type">Typea>
    <script type="text/javascript">
        $('#type').on('click', function() {
            sessionStorage.setItem("type", "type");
        });
    script>
body>
html>

2、新建html文件(跳转页面)


<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js">script>
    <title>Documenttitle>
head>
<body>
    <a class="type" onclick="type1()">商业物业a>
    <script type="text/javascript">
        function type1() {
            alert("出现了");
        }
    script>
    <script type="text/javascript">
        window.onload = function() { //页面加载完成后再执行
            var type = sessionStorage.getItem("type");
            console.log(type); //打印type值
            if (type == 'type') {
                var obj = "." + type;
                obj.onclick = type1(); //要触发的点击事件  $('#xxx').click()
                sessionStorage.setItem("type", ""); //清除 type 防止在test页面刷新后依然触发$('#xxx').click()
            }
        }
    script>
body>
html>

3、开心的执行了!
4、还有一种是类似get方式传参,但是我觉得太麻烦了,具体取值可以参考我的另一篇文章http://www.jianshu.com/p/3a449542a4d6,反正思路就是这样

你可能感兴趣的:(大前端-javascript)