带有元素的 CSS Content 属性

发布于 2024-12-16 02:52:55 字数 430 浏览 3 评论 0原文

我正在做一个小项目。

我希望使用 css content 属性和 :after 伪值在另一个元素内添加锚标记

例如,

.classname:after { content: '<a>hello</a>'; } 
// this will print "<a>hello</a>" 

我需要它做的是使其具有一个工作锚标记,指向给定的href。 我知道你可以使用类似 content: attr(title); 这样的东西,所以它将使用 .classname title 属性作为文本,我不知道不知道这是否可能,但如果可以的话那就太酷了。

I am working on a small project.

I wish to add an anchor tag <a> inside another element using the css content attribute and the :after pseudo.

e.g.

.classname:after { content: '<a>hello</a>'; } 
// this will print "<a>hello</a>" 

What I need it to do is make it have a working anchor tag, pointing to a given href.
I know you can use something like this content: attr(title); so it will use .classname title attribute as a text, I don't know if this is even possible but, it would be cool if it was.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-12-23 02:52:55

您不能使用 CSS :before:after 伪元素插入 HTML 元素。要添加 HTML 元素,您需要使用 JavaScript。

You can't use the CSS :before and :after pseudo-elements to insert HTML elements. To add HTML elements you need to use JavaScript.

迎风吟唱 2024-12-23 02:52:55

你不能害怕。你必须使用 javascript :(

一个简单的例子,将一个链接放入 ap 中,其 id 为 myP tho...以及一个 url 变量(实际上可以从任何值中获取)...

var myUrl = "http://www.glcreations.co.uk";

document.getElementById("myP").innerHTML = "<a href='" + myUrl + "'>A link for you to click on</a>";

You cant im afraid. You have to use javascript :(

A quick example of putting a link into a p with the id of myP tho... and a variable for a url (which could be obtained from any value really)...

var myUrl = "http://www.glcreations.co.uk";

document.getElementById("myP").innerHTML = "<a href='" + myUrl + "'>A link for you to click on</a>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文