I've been working with an HTML5 document with inline SVG and javascript animation.
I'd like to have a box pop up when the user clicks anywhere, and I'd like the box to go away when the user clicks somewhere that isn't the box. This means I can't use $(window).click()
, which works.
I've tried selecting the SVGs on top by giving them class names and using $(".svgclassname").click()
, but this doesn't seem to work. Neither does selecting individual ones with $("#svgname").click()
.
What is the problem?
(When I replace $(".eyesvg")
with $(window)
, a blue box appears near the cursor when the user clicks anywhere in the window.)
u can use jquery-svg plugin, like a charm:
This happens because SVG DOM spec differs a lot from HTML DOM.
SVG DOM is a different dialect, and some properties have same names but mean different things. For example, to get the className of an svg element, you use:
The properites affected by this are
These Animated properties are structs, with
baseVal
holding the same value you'd find in HTML DOM andanimatedVal
holding I am not sure what.SVG DOM is also missing some properties libraries depend on, such as
innerHTML
.This breaks jQuery in many ways, anything that depends on above properties fails.
In general, SVG DOM and HTML DOM do not mix very well. They work together just enough to lure you in, and then things break quietly, and another angel loses its wings.
I wrote a little jQuery extension that wraps SVG elements to make them look more like HTML DOM
It creates a wrapper around SVG objects that makes them look like HTML DOM to jQuery. I've used it with jQuery-UI to make my SVG elements droppable.
The lack of DOM interoperability between HTML and SVG is a total disaster. All the sweet utility libraries written for HTML have to be reinvented for SVG.
sometimes I don't get it... but actually it doesn't work with the class-selector. If you use the id $("#mysvg") or the element $("svg") it does work! Strange....
And it only works when you move the onClick script from the header to the body after the svg element! jquery can only bind the onclick when the element is declared before the binding.