Home > Web Front-end > HTML Tutorial > How to implement link selection in HTML? See content_html/css_WEB-ITnose for details

How to implement link selection in HTML? See content_html/css_WEB-ITnose for details

WBOY
Release: 2016-06-24 12:21:17
Original
1261 people have browsed it

HTML JavaScript

Use a nearly identical piece of code in multiple web pages:

First web page:
Menu 1
Menu 2
Menu 3< ;/a>
Menu 4
Second web page:
Menu 1
Menu 2
Menu 3
Menu 4
The third web page:
Menu 1
Menu 2
Menu 3
Menu 4
The fourth webpage:
Menu 1
Menu 2
Menu 3
Menu 4

I want to write the above code in a js In the file, the ID is determined by clicking on the link, which piece of code is used, such as the following structure:
if(...)
{
Menu 1
Menu 2
Menu 3< ;/a>
Menu 4
}
else if(...)
{
}
else if(...)
{
}

else
{
}
How to implement js?

Reply to discussion (solution)

function getAlist(alist){
Generate a tag based on alist
}

Called on each page
a page: getAlist(['a2','a3','a4]);
b page: getAlist(['a1','#','a3','a4]);

I don’t understand what you mean
If you generate a tag through js, you can use
var a=document.createElement("a");
a.id=.. .
a.href=...
a.innerHTML=...
You can try putting the content in a js object

Instead of generating it, it is better to control

function e(event){	var evt = event || window.event;	var target = evt.target || evt.srcElement;	window.location = target.id+".html";}/*   @linkId : 不需要跳转的 A 元素 ID*/function setLinks(linkId){	var menus = document.getElementById("menus").getElementsByTagName("a");	for(var i=0; i< menus.length; i++){		if(menus[i].id.toLowerCase() != linkId.toLowerCase()){			 if (window.attachEvent){				 menus[i].attachEvent('click',e)			 }else if(window.addEventListener){				 menus[i].addEventListener('click',e,false)			}		}		}	}window.onload = function(){    setLinks('a1');}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template