Javascript 如何为要重定向的每个链接编写脚本

Javascript 如何为要重定向的每个链接编写脚本,javascript,Javascript,因此,我尝试将用户从html链接和元素id标记重定向到使用javascript的其他页面。我已经知道了如何执行一个单一的重定向,但是在为多个链接编写代码时遇到了困难。到目前为止,我已经做到了: <head> <script type = "text/javascript" src="script2.js"></script> </head> <body> <a href="nickname.html" id="redirect

因此,我尝试将用户从html链接和元素id标记重定向到使用javascript的其他页面。我已经知道了如何执行一个单一的重定向,但是在为多个链接编写代码时遇到了困难。到目前为止,我已经做到了:

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>
HTML:

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>

我是否只需要启动更多函数并更改位置值、getElementById值和onclick值?

一个更好的方法是执行如下操作-这样重定向用户的逻辑就可以合理地靠近链接,因此,当页面将人们带到其他地方时,查看源代码的人不会感到难以置信的困惑

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>
<a href="nickname.html" id="redirect1" onclick="return initRedirect('Go to Salestax page?', 'nickname.html');">Some link</a>

onclick事件的一个好处是,如果返回false,它将阻止浏览器访问HREF中定义的链接。如果返回true,它将继续进行导航。如果使用此方法,则无需担心函数中的window.location,这将大大简化工作。所以你可以做:

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>
<a href="salestax.html" onclick="return confirm('Go to Salestax page?');">Salestax</a>

如果他们想继续,我会提示他们。他们单击“是”,它将继续运行,就像他们正常单击链接一样,他们单击“否”,它将阻止他们离开。这样,您就不必在HTML和javascript中复制链接的HREF

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>

您仍然可以动态绑定它,但是如果您是通过ID进行绑定的,那么与在HTML中定义onclick相比,我看不到任何优势。

首先,除非您试图防止用户丢失他们在当前页面上所做的更改,否则不清楚您为什么要创建此功能。但无论如何,这里有一个标准/基本方法:

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>
window.onload = function() {
     document.getElementById("redirect").onclick = redirectConfirmation("Go to Salestax page?");
     document.getElementById("redirect1").onclick = redirectConfirmation("Go to Nickname?");
};

redirectConfirmation = function(msg, urlOverride) {
    return function() {
        if ( confirm(msg) ) {
            window.location = urlOverride || this.href || "#"
        }
        return false;
    };
};
redirectConfirmation
可选地采用第二个参数,该参数可用于显式设置页面重定向到的url;否则,它将默认为所操作的锚定标记的
href
属性指定的URL(如果所有其他操作都失败,它将以“#”)优雅地失败

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>
如果您使用的是一个公共库,如jQuery,则可以简化事件注册,如下所示:

<head>
<script type = "text/javascript" src="script2.js"></script>
</head> 

<body>
<a href="nickname.html" id="redirect1">NickName</a>
<a href="salestax.html" id="redirect">Salestax</a>
<a href="http://www.w3schools.com" id="redirect2">W 3 Schools</a>

</body>
</html>
$(function() {
     $("#redirect").click( redirectConfirmation("Go to Salestax page?") );
     $("#redirect1").click( redirectConfirmation("Go to Nickname?") );
});

你到底为什么要这样做?你认为你的用户不知道他们想去哪里吗?除了你应该远离HTML嵌入式事件注册,并且该函数中的
confirm
没有任何作用。confirm函数是从问题中复制和粘贴的,我不同意反对嵌入式事件处理程序的观点——尽管我同意将行为和结构分开,但在某一点上,行为和结构之间需要有联系,这种方法使这种联系显而易见。链接做了一些意想不到的事情,这一事实从查看链接本身就可以清楚地看出,而在代码中,在链接可见之前,您需要查看其他地方,可能是不同的文件。同意,但如果他没有使用类似jQuery的东西,他可以使用选择器轻松绑定所有这些事件,一次只能用一个ID绑定所有东西,这太疯狂了。最好的重构方法是添加确认文本作为标题,并使用一点jQuery,但要避免太多的更改。