Html 使用 css 将链接添加到元素的 href

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16910265/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:01:59  来源:igfitidea点击:

Adding link a href to an element using css

htmlcss

提问by amit

Can I add an <a href="">link to an HTML element using CSS. As I have an image which I want to add a link to a particular page. Can any one help me in doing this?

我可以<a href="">使用 CSS添加指向 HTML 元素的链接吗?因为我有一张图片,我想添加一个指向特定页面的链接。任何人都可以帮助我做到这一点吗?

回答by Selvamani

No. Its not possible to add link through css. But you can use jquery

不,它不可能通过 css 添加链接。但是你可以使用jquery

$('.case').each(function() {
  var link = $(this).html();
  $(this).contents().wrap('<a href="example.com/script.php?id="></a>');
});

Here the demo: http://jsfiddle.net/r5uWX/1/

这里的演示:http: //jsfiddle.net/r5uWX/1/

回答by Elegant Coder

You cannot simply add a link using CSS. CSS is used for styling.

您不能简单地使用 CSS 添加链接。CSS 用于样式化。

You can style your using CSS.

您可以使用 CSS 设置样式。

If you want to give a link dynamically to then I will advice you to use jQuery or Javascript.

如果您想动态提供链接,那么我建议您使用 jQuery 或 Javascript。

You can accomplish that very easily using jQuery.

您可以使用 jQuery 非常轻松地完成此操作。

I have done a sample for you. You can refer that.

我已经为你做了一个样本。你可以参考一下。

URL : http://jsfiddle.net/zyk9w/

网址:http: //jsfiddle.net/zyk9w/

$('#link').attr('href','http://www.google.com');

This single line will do the trick.

这一行就可以解决问题。

回答by Chris

You dont need css for this..

你不需要 css 为此..

     <img src="abc"/>

now with link:

现在有链接:

     <a href="#myLink"><img src="abc"/></a>

Or with jquery later on, you can use the wrap property, see this questions answer:

或者稍后使用 jquery,您可以使用 wrap 属性,请参阅以下问题答案:

how to add a link to an image using jquery?

如何使用jquery向图像添加链接?