【问题标题】:How to remove underline on hover from <text> in <a> element in svg如何从 svg 的 <a> 元素中的 <text> 中删除悬停时的下划线
【发布时间】:2019-05-27 02:28:57
【问题描述】:

我无法摆脱悬停在 svg 代码中的链接上的下划线。 有问题的网站:https://www.lokalfilm.com/svgtest2/ 下划线出现在地图上的所有标签上,我提供了下面代码的 sn-p 作为参考(对于其中一个标签)

代码是从 adobe illustrator 生成的。它是 SVG tiny 1.1,因为 SVG 1.1 创建的代码对于我来说太长而无法在不进入开发人员模式的情况下使用。我用的是squarespace,我不是专业的开发者。

我已经尝试过之前关于 SO 的问题中提供的解决方案(我发现唯一一个相关的问题),但它不起作用: svg <text> element inside <a> element gets underline on hover in Chrome

<g id="Tekst">
<a xlink:href="https://www.lokalfilm.com/sunde" >
<text transform="matrix(1 0 0 1 352.7658 322.5889)" font- 
family="'BlockBE-Regular'" font-size="11px">Sunde</text>
</a>
</g>

【问题讨论】:

    标签: svg hyperlink underline


    【解决方案1】:

    我想您的 CSS 样式中有 &lt;a&gt; 元素。要仅为 SVG 覆盖这些规则,您可以使用 @namespace 来设置带有 xlink:href 属性的 &lt;a&gt; 元素的样式。 a[xlink|href]{text-decoration:none;}

    /* defines the namespace for the xlink*/
    @namespace xlink "http://www.w3.org/1999/xlink";
    /* a stile for all the a elements in the document*/
    a{text-decoration:underline;}
    
    /*Styling only the a elements in the svg*/
    a[xlink|href]{text-decoration:none;}
    <svg viewBox = "350 310 33 30">
    <g id="Tekst">
    <a xlink:href="https://www.lokalfilm.com/sunde" >
    <text transform="matrix(1 0 0 1 352.7658 322.5889)" font- 
    family="'BlockBE-Regular'" font-size="11px">Sunde</text>
    </a>
    </g>
    </svg>

    如果您没有带有&lt;a&gt; 元素的foreignObjects,您也可以使用svg a 选择器。

    更新:

    OP 正在评论:

    但是我无法访问该代码,因此我必须调整 svg 代码以覆盖它...我只是不确定要插入什么以及将其插入到我的 svg 代码中的什么位置

    这就是您如何将 css 样式添加到您的 SVG:在 SVG 中您可以添加以下代码块:

    接下来是一个示例,其中 SVG 外部的 &lt;a&gt; 元素在鼠标悬停时带有下划线,但 SVG 内部的 &lt;a&gt; 元素没有:

    /* a stile for all the a elements in the document*/
    a{text-decoration:none;}
    a:hover{text-decoration:underline;}
    svg{border:1px solid; margin:1em 0;width:80vh}
    <p><a href="https://stackoverflow.com">A link in the SVG element</a></p>
    
    
    <svg viewBox="350 310 33 30">
    
    <style type="text/css">
            <![CDATA[
            /* defines the namespace for the xlink*/
            @namespace xlink "http://www.w3.org/1999/xlink";
            /*Styling only the a elements in the svg*/
            a[xlink|href]:hover{text-decoration:none;}
            ]]> 
    </style>
      
    <g id="Tekst">
    <a xlink:href="https://www.lokalfilm.com/sunde" >
    <text transform="matrix(1 0 0 1 352.7658 322.5889)" font- 
    family="'BlockBE-Regular'" font-size="11px">Sunde</text>
    </a>
    </g> 
      
    </svg>

    【讨论】:

    • 既然 id 为 Tekst 的组包含所有 a 元素,像 #Tekst a:hover { text-decoration: none; } 这样的东西还不够吗?
    • 感谢您的回复。我试图绕开你的答案,但我没有足够的知识来理解该怎么做。当我在 jsfiddle 中运行代码时,它显示的链接上没有下划线。所以它似乎是我正在使用的 squarespace 模板中最重要的样式元素。但是我无法访问该代码,因此我必须调整 svg 代码以覆盖它。这就是你要回答我的@enxaneta,我只是不确定要插入什么以及在我的 svg 代码中插入的位置。
    • @FredericKlein - 您的建议是否应该放在标签中,例如
    • 我已经更新了解释如何在 SVG 元素中添加样式的答案。请看一看。
    • 您的第一个代码没有做任何事情,但又一次 - 我不确定我是否将它正确插入到我的代码中(颜色保持黑色,不确定它是否应该在某个标签内)。您提供的最后一个代码为我解决了这个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2011-11-03
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2013-09-18
    相关资源
    最近更新 更多