【发布时间】:2014-07-16 16:09:10
【问题描述】:
我们有一个 SVG 图,其节点定义如下:
<!-- sac -->
<g id="node37" class="node"><title>sac</title>
<polygon fill="none" stroke="black" points="692.5,-952 692.5,-1004 765.5,-1004 765.5,-952 692.5,-952"/>
<text text-anchor="middle" x="729" y="-987.4" font-family="Times Roman,serif" font-size="14.00">sac</text>
<polyline fill="none" stroke="black" points="692.5,-978 765.5,-978 "/>
<text text-anchor="middle" x="729" y="-961.4" font-family="Times Roman,serif" font-size="14.00">sacsip.o</text>
</g>
我希望能够添加 onclick 元素,该元素知道被点击的元素具有什么文本,即“saccsip.o”。我们尝试过类似
<script>
function whichElement(event){
var tname
tname= event.target
alert("You clicked on " + tname)
}
</script>
但它只会返回元素,而不是文本本身。如果元素是 HTML,我们可以检索信息,但使用 SVG 时我们没有成功。可能吗?有没有其他办法?
【问题讨论】:
-
获取被点击节点的textContent值。
-
您在警报框中实际看到了什么?
-
@MattEllen 我得到“你点击了 [object SVGTextElement]”
标签: javascript html svg dot