如何将svg形状与svg文本合并(How to merge svg shape with a svg text)

我在尝试将svg文本与svg形状合并时遇到问题。 这是我当前的代码,白色条只在我的指针在框中时变为灰色,但是当它在字母上时,框变回白色。 如何使它保持灰色,即使我的鼠标在文本上,直到我将指针移离文本和框。 我已经尝试将函数放在<g><a><svg>标签中,但它根本不起作用。

<script>
    function ontop(x){
        x.style.fill = "#c8cace"
    }

    function notOnTop(x){
        x.style.fill = "white"
    }
</script>

    <svg>
    <g>
        <a href="https://google.com" target="_blank" >
        <rect height="50px" width="350px" x="70%" y="14%" fill="white" rx="5px" onmouseenter="ontop(this)" onmouseleave="notOnTop(this)" />
        <text x="72%" y="22%" font-size="20" fill="black" >Google</text>
        </a>
    </g>
    </svg>

Im having problems trying to merge a svg text with an svg shape. This is my current code, the white bar only turn grey when my pointer is in the box, but when it is on the letter, the box turned back white. How do i make it so that it will remain grey even though my mouse is on the text until I move my pointer away from the text and the box. I've tried putting the functions in the <g>,<a>,<svg> tags but it simply won't work.

<script>
    function ontop(x){
        x.style.fill = "#c8cace"
    }

    function notOnTop(x){
        x.style.fill = "white"
    }
</script>

    <svg>
    <g>
        <a href="https://google.com" target="_blank" >
        <rect height="50px" width="350px" x="70%" y="14%" fill="white" rx="5px" onmouseenter="ontop(this)" onmouseleave="notOnTop(this)" />
        <text x="72%" y="22%" font-size="20" fill="black" >Google</text>
        </a>
    </g>
    </svg>
2023-02-05 22:02

满意答案

给文本指定属性pointer-events:none,以便鼠标忽略它。

请注意,您不需要使用javascript来悬停CSS:hover会这样做。

rect {
    fill: white;
}

rect:hover {
    fill: #c8cace;
}
<svg>
    <g>
        <a href="https://google.com" target="_blank" >
        <rect height="50px" width="350px" x="70%" y="14%" rx="5px" />
        <text x="72%" y="22%" font-size="20" fill="black" pointer-events="none">Google</text>
        </a>
    </g>
</svg>


Give the text the property pointer-events: none so that it is ignored by the mouse.

Note that you don't need javascript to do hovering CSS :hover will do that.

rect {
    fill: white;
}

rect:hover {
    fill: #c8cace;
}
<svg>
    <g>
        <a href="https://google.com" target="_blank" >
        <rect height="50px" width="350px" x="70%" y="14%" rx="5px" />
        <text x="72%" y="22%" font-size="20" fill="black" pointer-events="none">Google</text>
        </a>
    </g>
</svg>

相关问答

更多

如何将文本转换为SVG路径?(How to convert text to SVG paths?)

我创建了自己的类来处理SVG字体文件并将文本转换为字形。 使用示例: include "SVGFont.php"; $svgFont = new SVGFont(); $svgFont->load("/path/to/font.svg"); $result = $svgFont->textToPaths("Simple text", 20); 示例结果: <g transform="scale(0.009765625) translate(0, 0)"><path transform="tran...

如何获得SVG文本的“crispEdges”?(How to get “crispEdges” for SVG text?)

对于非常清晰的边缘,您可以使用过滤器来分隔文本。 <svg width="400px" height="400px"> <defs> <filter id="crispify"> <feComponentTransfer> <feFuncA type="discrete" tableValues="0 1"/> </feComponentTransfer> </filter> </defs> <text filter="url(#crispify)" font-size="60...

SVG:形状上的文本禁用背景形状上的动画(SVG: text over a shape disables animation on background shape)

使用pointer-events: none可以使文本元素对您的悬停“透明”。 div, svg { background-color: grey; height: 100px; width: 600px; } text { pointer-events: none; } <h1> test </h1> <div> <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <circle id...

如何将svg形状拖到另一个svg中以便保存(how to drag an svg shape into another svg so that it can be saved)

需要做几件事来完成这项工作: 当它看起来像将圆圈拖到红色的#save SVG中时,实际上只是将它拖到该框的顶部 。 圆圈仍然只是#canvas SVG的一个孩子。 您需要检测何时将圆圈放置在#save区域内,然后将其从#canvas移到#save自己。 看看新的dragIntoBoard()函数,它通过测量圆圈被放下时鼠标指针的位置来实现。 在读取SVG xml进行保存时,您需要使用XMLSerializer而不是save.innerHTML 。 .innerHTML将包含#save 内的所有元素...

SVG:点击底层svg(SVG: Click underlying svg)

您需要为包装器元素( div#item1 )和SVG( svg.crossSvg )禁用指针事件,因为SVG具有不同的指针上下文。 如果你将它添加到CSS的末尾,它可以正常工作: #item1 { pointer-events: none; } #item1 .crossSvg { pointer-events: none; } .itemSquare { pointer-events: all; } (尽管在选择器中使用ID进行样式设置不是最佳做法) 编辑:更新了您的小提琴,以反...

用另一个SVG形状填充SVG形状(Fill SVG shape with another SVG shape)

您想使用SVG模式 。 看这个例子 : <svg viewBox="0 0 800 400" xmlns="http://www.w3.org/2000/svg"> <defs> <pattern id="TrianglePattern" patternUnits="userSpaceOnUse" x="0" y="0" width="100" height="100" viewBox="0 0 10 10" > <pat...

使用SVG和JS创建曲线文本(Creating Curved Text with SVG and JS)

xlink:href属性不能使用setAttribute设置,因为该方法只能在null命名空间中设置属性,而xlink:href在xlink命名空间中。 请改用setAttributeNS,并指定xlink名称空间http://www.w3.org/1999/xlink作为第一个参数。 The xlink:href attribute cannot be set with setAttribute as that method can only set attributes in the null...

如何将svg形状与svg文本合并(How to merge svg shape with a svg text)

给文本指定属性pointer-events:none,以便鼠标忽略它。 请注意,您不需要使用javascript来悬停CSS:hover会这样做。 rect { fill: white; } rect:hover { fill: #c8cace; } <svg> <g> <a href="https://google.com" target="_blank" > <rect height="50px" width="350...

如何从SVG中的形状中剪切文本?(How can I cut text from a shape in SVG?)

创建一个蒙版,通过文本元素将其中的文本,然后使用你想要剪的洞中的形状的面具。像这样的... head, body { width:100%; height:100%; } <svg width="100%" height="100%" viewBox="0 0 200 200"> <defs> <mask id="sample" maskUnits="userSpaceOnUse"> <rect width="100%" height="100%" f...

d3js svg同一行上的文本和形状(d3js svg text and shapes on same line)

我尝试使用tspan.node()获取tspan维度.getComputedTextLength()但是这会返回一个错误,我认为因为它没有在调用时呈现。 我只是使用text.node()。getBBox()来获取每个文本块的尺寸: function renderSVGText(){ var svgArea = d3.select("svg#svg_area_id"); var group = svgArea.append("g") .attr("width", "...

相关文章

更多

HTML5 SVG【HTML5教程 - 第八篇】

什么SVG?SVG是Scalable Vector Graphics的缩写用来定义web中基于矢量的图 ...

HTML5 Canvas vs. SVG【HTML5教程 - 第九篇】

SVG基于XML,这意味着每一个元素在SVG DOM中都是存在。你可以使用javascript来处理其 ...

最新问答

更多

Python / IPython奇怪的不可重现列表索引超出范围错误(Python/IPython strange non reproducible list index out of range error)

你得到IndexError的原因是你的输入文件显然不是完全用制表符分隔的。 这就是为什么当您尝试访问它时, splits[1]没有任何内容。 您的代码可以使用一些重构。 首先,你正在重复使用if -checks,这是不必要的。 这只是将cds0到7个字符,这可能不是你想要的。 我将以下内容放在一起,以演示如何重构您的代码,使其变得更加pythonic和干燥。 我无法保证它能够与您的数据集一起使用,但我希望它可以帮助您了解如何以不同的方式执行操作。 to_sort = [] # W

故事板从表格单元中延伸出来并显示其详细信息披露按钮(storyboard segue from a tablecell an its Detail Disclosure Button)

我不认为你可以链接一个特定的细节披露按钮瓦特/赛格。 我的故事板是非常程序化的B / C我使用了很多定制CGRect等。 所以我倾向于使用这样的东西: -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"ViewControllerIdentifer"

我们可以将Gherkin功能文件与testcomplete集成(Can we integrate Gherkin feature files with testcomplete)

TestComplete不支持Gherkin功能文件。 但是,TestComplete具有SDK,因此可以自己为此创建扩展。 TestComplete does not support Gherkin feature files. However, TestComplete has SDK so it is possible to create an extension for this by yourself.

TrustAllCertificatesCallback被忽略(TrustAllCertificatesCallback is ignored)

我没有做过这样的事情,但看看我认为可以看出错误的例子。 试试这个代码: static class Program { static void Main() { var tcpclient = new TcpClient("remote.example.com", 443); var tcpstream = tcpclient.GetStream(); var sslstream = new SslStream(tcpstream,

返回嵌套元素的索引(Return index of nested element)

您需要获取父li元素的索引。 否则,您将获得列表项内锚点的索引,该索引始终为零。 $(this.parentNode).index(); You need to get the index of the parent li element. Otherwise you are getting the index of the anchor inside the list item, which will always be zero. $(this.parentNode).index();

在数组中重新编号元素的有效方法(Efficient way of re-numbering elements in an array)

您需要多次迭代列表,我认为没有办法解决这个问题。 毕竟,在开始更改元素(第二遍)之前,首先必须确定不同元素的数量(第一遍)。 但是请注意,由于对index的重复调用而not in列表中具有O(n),因此可能具有最多O(n ^ 2)的不同元素的数量。 或者,您可以使用dict而不是value_map的list 。 字典比列表具有更快的查找速度,因此,复杂性应该确实在O(n)的数量级上。 您可以使用(1)字典理解来确定旧值到新值的映射,以及(2)用于创建更新子项的列表理解。 value_map =

Express app定义的`static`文件夹无法正常工作(Express app defined `static` folder not working properly)

选项1 你可以改变这一行: app.use( express.static(__dirname + '/puplic')); //my public folder. 至 app.use('/oneTime', express.static(__dirname + '/puplic')); //my public folder 选项2 我假设你在公共文件夹中有一个js文件夹,然后你需要更改你的HTML代码:

使用node.js和socket.io每秒广播一次(Using node.js and socket.io to broadcast every second)

对于计时器值,请在服务器端本身每秒更新本地计时器。 每当有任何用户进来时,给他这个值以及计时器的总值。 然后客户端将根据dandavis评论在本地启动自己的计时器,但在服务器端保留一些间隔,如15或10秒,服务器将广播当前计时器值,以便客户端相应地进行同步。 简而言之,服务器将每隔10(n:你决定)秒后广播,但它将在本地每秒更新定时器变量。 每当客户端进入时,它将获得总计时器值和当前计时器值。 广播当前出价的休息功能可以以正常方式完成。 For timer value, keep updatin

如何让XMLSerializer将命名空间添加到嵌套对象中的属性?(How do I get the XMLSerializer to add namespaces to attributes in nested objects?)

IXmlSerializable也许? 注意我还添加了(对A ): [XmlElement("A", Namespace = "http://www.example.com/namespace")] public TestSoapHeaderTypeValuePair A {...} 代码如下: public class TestSoapHeaderTypeValuePair : IXmlSerializable { private string _type; private