0

在我后面的代码中,我使用常量 String 和 String.Format 来创建一系列 svg 元素,如下所示:

const string BoxTemplate = "<rect x=\"{0}\" y=\"{1}\" height=\"{2}\" width=\"{3}\" fill=\"{4}\" stroke=\"#707070\" onmouseover=\"showToolTip()\"/>";
string s = String.Format(BoxTemplate, x, y, h, w, color);

然后我使用文字控件来渲染图像。

我想在元素中包含数据或至少一个数据键,以供鼠标悬停或使用其他事件。我考虑过将 id 设置为 datakey,但在某些情况下,不止一个图像会引用相同的数据。

什么是让我的 javascript 函数识别“鼠标悬停”的元素并访问其数据/数据键的最佳方法。

4

1 回答 1

0

您可以将this用作函数的参数。如果您在渲染时有附加参数,您还可以添加一个附加参数。

这对我有用:

<html>

    <script>
        function showToolTip(obj, datakey) {
            alert("ID=" + obj.id + " datakey=" + datakey);
        }
    </script>
    <body>
        <svg>
            <rect id="The_ID" x="10" y="10" height="100" width="100" fill="black" stroke="#707070" onmouseover="showToolTip(this, 'datakey1')"/>
        </svg>
        <svg>
            <rect id="The_other_ID" x="110" y="10" height="100" width="100" fill="black" stroke="#707070" onmouseover="showToolTip(this, 'datakey2')"/>
        </svg>
    </body>
</html>
于 2013-02-01T13:22:04.657 回答