使用 css 更改 html 标签的 href
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15356717/
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
Changing the href of an html tag with css
提问by kais mellah
I need to change this A tag from
我需要改变这个 A 标签
<a href="contact.html" >Contact</a>
to
到
<a href="tel:+13174562564" >Contact</a>
with only Css if it's possible or like this
如果可能或像这样,只使用 CSS
<a href="tel:+13174562564" >+13174562564</a>
i have a lot of pages and i don't want to edit all of them it will take a life time that's why i need to do it in CSS and i have no JS linked to them
我有很多页面,我不想编辑所有这些页面,这将花费一生的时间,这就是为什么我需要在 CSS 中完成它并且我没有链接到它们的 JS
回答by jnovack
CSS is display only, you cannot modify the document object model with it. Sorry, this cannot be done. I wish I had a better answer, but 'it cannot be done' is the only answer.
CSS 仅用于显示,您不能用它修改文档对象模型。抱歉,这是无法做到的。我希望我有一个更好的答案,但“这是不可能的”是唯一的答案。
回答by Anirudh Ramanathan
CSS is for presentationand cannot be used to modify the HTML markup in the way you intend. You need JavaScript for this.
CSS 用于演示,不能用于以您想要的方式修改 HTML 标记。为此,您需要 JavaScript。
It should be a fairly short script.
它应该是一个相当短的脚本。
回答by kiwi
one solution is to hide a tag and put another
一种解决方案是隐藏一个标签并放置另一个
<div id="tag1">
<a href="#tag1">link1</a>
<a href="#tag2">link2</a>
</div>
css :
css :
a[href="#tag1"] {
display:block;
}
a[href="#tag2"] {
display:none;
}
#tag1:target a[href="#tag1"]{
display:none;
}
#tag1:target a[href="#tag2"]{
display:block;
}
I use this method for responsive "buttons" of my menu bar
我将此方法用于菜单栏的响应“按钮”