javascript 如何在 HTML 中创建条件超链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9075540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create conditional hyperlink in HTML
提问by user1179678
I want to link my page with another page on condition.
我想在有条件的情况下将我的页面与另一个页面链接起来。
Suppose I have 3 HTML pages, namely 1.html, 2.htmland 3.html. What I want is that if 1.htmlis loaded then load page 2.html; If 1.htmlis not loaded then load 3.html.
假设我有 3 个 HTML 页面,即1.html、2.html和3.html。我想要的是,如果1.html加载然后加载页面2.html;如果1.html未加载,则加载3.html。
please help.
请帮忙。
回答by nnnnnn
I can't follow your explanation about pages 1, 2 and 3, but in a general sense you can have a hyperlink go to different URLs depending on some condition(s) by handling its "onclick" event to cancel the default navigation and do it from JavaScript instead:
我无法理解您对第 1、2 和 3 页的解释,但在一般意义上,您可以通过处理其“onclick”事件来取消默认导航并根据某些条件将超链接转到不同的 URL它来自 JavaScript:
<a href="defaulturlhere" onclick="doClick(); return false;">My link</a>
<script>
function doClick() {
if (someCondition || someOtherCondition)
window.location.href = "firstURLhere";
else
window.location.href = "alternativeURLhere";
}
</script>
The URL specified in the anchor's href
attribute will be used if JavaScript is disabled. Otherwise, the doClick()
function is called to decide which URL to navigate to. Of course the function can be as simple or complicated as you need.
href
如果禁用 JavaScript,将使用锚点属性中指定的 URL 。否则,将doClick()
调用该函数来决定导航到哪个 URL。当然,该功能可以根据您的需要简单或复杂。
The onclick
needs to return false;
to cancel the default behaviour of a click on the anchor because (obviously) the default is to navigate to the URL in the href
attribute.
该onclick
来的需求return false;
,取消对锚点击的默认行为,因为(明显)的默认值是导航到该URLhref
属性。
回答by Jashwant
I am not fully sure what you want to achieve.
我不完全确定你想要实现什么。
I think you want to show hyperlink on a page only if some other pages are opened earlier.
我认为只有在之前打开了其他一些页面时,您才希望在页面上显示超链接。
If this is the case, you can create cookies on window.load of page 1, and check if that cookie is set on windolow.onload event of page 2.
如果是这种情况,您可以在第 1 页的 window.load 上创建 cookie,并检查该 cookie 是否设置在第 2 页的 window.onload 事件上。
If cookie is set, create a dynamic hyperlink on page 2 to redirect to page 3. If cookie is not set, do not create a link.
如果设置了 cookie,则在第 2 页上创建一个动态超链接以重定向到第 3 页。如果未设置 cookie,则不要创建链接。
You may also show / hide hyperlink (instead of dynamically creating) depeding on whether cookie is set or not. This is an easy and crossbrowser way if you are not using jQuery.
您还可以根据是否设置 cookie 来显示/隐藏超链接(而不是动态创建)。如果您不使用 jQuery,这是一种简单且跨浏览器的方式。
回答by edgarator
It should be something along the lines of:
If you add the script at the bottom of the page, the javascript will search for all the <a>
tags and compare them to the current url
. If theres a match it will set its style to invisible.
它应该是这样的:如果在页面底部添加脚本,javascript 将搜索所有<a>
标签并将它们与当前的url
. 如果有匹配项,它会将其样式设置为不可见。
<script>
linkNodes = document.getElementsByTagName("a");
for(i = 0; i < linkNodes.length; i++){
if(linkNodes[i].getAttribute("href") == document.url){
linkNodes[i].style.visibility= "hidden";
}
}
</script>
This way if you are in 1.html, 2.html and 3.html are displayed but not 1.html itself. the same happens for 2.html which would show only 1.html and 3.html... etc.
这样,如果您在 1.html 中,将显示 2.html 和 3.html 而不是 1.html 本身。2.html 也会发生同样的情况,它只会显示 1.html 和 3.html ......等。