首页 > 解决方案 > 我想使用 CSS 更改类的属性值

问题描述

我想使用 CSS 更改类的属性值。例如:html代码为:

<a href="https://www.google.com/" title="My Account"> My Account </a>

假设我需要将 href 更改为,href="https://www.stackoverflow.com/"或者我想将标题更改为title="Go To Your Account"

我想使用 CSS 来做到这一点。我使用此 CSS 代码更改标题属性,但它隐藏了元素本身

    a[title="My Account"]{
          visibility:hidden;
    }
    a[title="My Account"]:before{
          content: "Go To Your Account|";
    }

标签: htmlcssattributes

解决方案


您可以使用 javascript 来做到这一点。您还可以使用 jquery 库来少写多写代码

<a id="my_acc" href="#acc" title="My Account"> My Account </a>


<script>
var my_acc = document.getElementById("my_acc");
my_acc.addEventListener("click", function(){
    this.setAttribute("title", "Go to your Account");
});
</script>

N:您需要在 html 中的 body 标记末尾添加 javascript 以使用事件侦听器


推荐阅读