我正在我的Mvc应用程序中使用SVG和jQuery。我在我的页面上绘制了一系列矩形,我想做的是为每个矩形附加一个点击或鼠标悬停事件,比如弹出一个警告框。
到目前为止,我尝试过这样的代码:
$("rect[id='Y6']").attr('onclick', function() { alert("Hello!") });
和
$("rect[id='Y6']").live('click', function() {
alert("Hello!");
};
但是很遗憾,这些事件都不适用于此控件。有谁知道怎么做吗?
编辑:
下面我添加了我正在使用的 JavaScript 代码:
<script type="text/javascript">
/*function resetSize(svg, width, height) {
svg.configure({ width: width || $(svg._container).width(),
height: height || $(svg._container).height()
});
}*/
function onLoad(svg, error) {
//svg.text(10, 20, error || 'Loaded into ' + this.id);
//resetSize(svg, null, null); //'100%', '100%');
}
$(function() {
$('#layout').svg({});
var svg = $('#layout').svg('get');
svg.load('<%= Url.Action("Layout", new{ id = Model.Id }) %>', { //<%= Url.Content("~/media/svg/lion.xml") %>', {
addTo: false,
changeSize: false,
onLoad: onLoad
});
});
$('rect#Y6').click( function(){
alert('hello');
});
</script>
< p >这些矩形是从SVG图片中加载的。< /p >
$(document).on('click touch', '.button', function () { ... });
解决了这个问题。 - Dylan Valade