开发者

Click event for SVG rectangle

开发者 https://www.devze.com 2023-01-22 08:37 出处:网络
I\'m using SVG together with jQuery in my Mvc Application. I draw a series of rectangles on my page and what I would like t开发者_运维技巧o do is attach a click or mouseover event for each rectangle,

I'm using SVG together with jQuery in my Mvc Application. I draw a series of rectangles on my page and what I would like t开发者_运维技巧o do is attach a click or mouseover event for each rectangle, for say, popup an alert box. I've tried something like this, so far:

$("rect[id='Y6']").attr('onclick', function() { alert("Hello!") });

and

$("rect[id='Y6']").live('click', function() {    
    alert("Hello!");
};

But sadly none of this events really worked for this control. Does anyone know how to do this?

EDIT:

I'm adding the javascript code I'm using below:

<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>

The rectangles are loaded from a svg picture.


Your code to add the click handler needs to be part of the onLoad function - with that change it works for me.


Try this:

$('rect#Y6').click( function(){
  alert('hello');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号