我有一个HTML文件,其中有几个链接指向相同的URL。有没有办法只指定一次URL,而不是在每个锚点中指定URL?目前,如果我更改URL,我会在每个锚点中手动更改它。我会考虑Javascript解决方案,但我更喜欢更简单和轻量级的解决方案。以下是包含两个指向google.com的链接的代码示例:
<HTML>
<HEAD>
</HEAD>
<BODY>
<P>
<A HREF="https://www.google.com/">Preferred search engine</A>
</P>
<HR>
<P>
<A HREF="https://www.google.com/">Google</A>
</P>
</BODY>
</HTML>
发布于 2016-12-07 05:56:36
您可以使用元素:
例如:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
<base href="https://www.google.com/">
</head>
<body>
<p>
<a href="">Preferred search engine</a>
</p>
<a href="">Google</a>
</p>
</body>
</html>
您指定的任何链接都将是相对于基URL的,因此如果您使用<a href="foo.html">Preferred search engine</a>
,则链接将是https://www.google.com/foo.html
发布于 2016-12-07 05:55:36
你必须使用javascript来做到这一点。jQuery会做得很好:
$(function() {
$('a').attr('href','https://www.google.com');
});
如果URL改变了,你也可以调用同样的东西。
如果你不想使用jQuery,这是等价的js代码:
<script>
function ChangeHref(){
document.getElementById("a").setAttribute("onclick", "location.href='https://www.google.com'");
}
</script>
你可以从你想要的任何地方触发changehref
。
顺便说一句:上面的函数等于document.ready
。
https://stackoverflow.com/questions/41005598
复制相似问题