Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Selector + SVG Incompatible?

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

like image 408
Will Avatar asked Jul 20 '10 21:07

Will


2 Answers

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:

svg.className.baseVal 

The properites affected by this are

className is SVGAnimatedString height,width, x, y, offsetWidth, offsetHeight are SVGAnimatedLength 

These Animated properties are structs, with baseVal holding the same value you'd find in HTML DOM and animatedVal 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

(function (jQuery){     function svgWrapper(el) {         this._svgEl = el;         this.__proto__ = el;         Object.defineProperty(this, "className", {             get:  function(){ return this._svgEl.className.baseVal; },             set: function(value){    this._svgEl.className.baseVal = value; }         });         Object.defineProperty(this, "width", {             get:  function(){ return this._svgEl.width.baseVal.value; },             set: function(value){    this._svgEl.width.baseVal.value = value; }         });         Object.defineProperty(this, "height", {             get:  function(){ return this._svgEl.height.baseVal.value; },             set: function(value){    this._svgEl.height.baseVal.value = value; }         });         Object.defineProperty(this, "x", {             get:  function(){ return this._svgEl.x.baseVal.value; },             set: function(value){    this._svgEl.x.baseVal.value = value; }         });         Object.defineProperty(this, "y", {             get:  function(){ return this._svgEl.y.baseVal.value; },             set: function(value){    this._svgEl.y.baseVal.value = value; }         });         Object.defineProperty(this, "offsetWidth", {             get:  function(){ return this._svgEl.width.baseVal.value; },             set: function(value){    this._svgEl.width.baseVal.value = value; }         });         Object.defineProperty(this, "offsetHeight", {             get:  function(){ return this._svgEl.height.baseVal.value; },             set: function(value){    this._svgEl.height.baseVal.value = value; }         });     };      jQuery.fn.wrapSvg = function() {         return this.map(function(i, el) {             if (el.namespaceURI == "http://www.w3.org/2000/svg" && !('_svgEl' in el))                 return new svgWrapper(el);             else                 return el;             });         }; })(window.jQuery); 

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.

like image 199
Aleksandar Totic Avatar answered Sep 22 '22 10:09

Aleksandar Totic


u can use jquery-svg plugin, like a charm:

<script>     //get svg object, like a jquery object     var svg = $("#cars").getSVG();     //use jquery functions to do some thing     svg.find("g path:first-child()").attr('fill', color); </script> 
like image 45
mohammadreza berneti Avatar answered Sep 24 '22 10:09

mohammadreza berneti