【发布时间】:2014-06-16 19:20:34
【问题描述】:
此代码在单击组件按钮时动态添加 svg 图像。之后我 尝试将警报功能传递给动态创建的组件,但它不响应单击。请帮忙。我尝试使用 id、class、path 传递函数。
<html>
<head>
<title>buttons</title>
<style>
input[type="button"]{
color:#050;
font: old 84% 'trebuchet ms',helvetica,sans-serif;
background-color:#fed;
border:1px solid;
border-color: #696 #363 #363 #696;
}
</style>
</head>
<body>
<svg class="svg-main" style="height:60%;" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink">
</svg>
<input type="button" class="Mouse" value="Cursor"/>
<input type="button" class="Component" value="Resistor"/>
<script src="jquery-1.11.1.js"></script>
<script type="text/javascript" src="d3.v2.js"></script>
<script type="text/javascript" src="jquery-svgpan.js"></script>
<script>
$(document).ready(function(){
var $svg= $('.svg-main');
$(".Component").click(function(){
$(SVG('path'))
.attr('d','M10 15 l15 0 l2.5 -5 l5 10 l5 -10 l5 10 l5 -10 l5 10 l2.5 -5 l15 0')
.attr('fill', 'none')
.attr('stroke', 'black')
.attr('stroke-width', 2)
.attr('stroke-linejoin' , 'bevel')
.attr('id' , 'res')
.appendTo($svg);
});
$("#res").click(function(){
alert("hi");
});
function SVG(tag)
{
return document.createElementNS('http://www.w3.org/2000/svg',tag);
}
});
</script>
</body>
</html>
【问题讨论】: