Próbuję zrobić prostokąt SVG wokół tekstu SVG. Kiedy chcę używać .width()
lub .height()
w tekście SVG, Chrome zwraca to, czego oczekuję, ale Firefox tego nie robi. Oto link do stworzonej przeze mnie wersji demo: jsfiddle.Szerokość i wysokość JQuery nie działa na svg w firefox
$(document).ready(function(){
var $rect = $(document.createElementNS('http://www.w3.org/2000/svg', 'rect'));
var x = 50;
var y = 50;
var fontSize = 10;
$rect.attr({
'x' : x,
'y' : y,
'stroke' : 'black',
'stroke-width' : 1,
'fill' : 'white'
});
$rect.appendTo($("#svgArea"));
var $svgText = $(document.createElementNS('http://www.w3.org/2000/svg', 'text'));
$svgText.attr({
'x': x,
'y': y,
'font-family' : 'verdana',
'font-weight': 'bold',
'font-size' : fontSize
});
var node = document.createTextNode("Lorem Ipsum");
$svgText.append(node);
$svgText.appendTo($("#svgArea"));
var textWidth = $svgText.width();
var textHeight = $svgText.height();
$rect.attr({
'x': x,
'y': y - textHeight + 3,
'width' : textWidth + 2,
'height': textHeight
});
});
HTML
<svg height="200px" width="200px" id="svgArea">
</svg>
CSS
#svgArea{
border: 1px solid black;
}
jQuery jest przeznaczony do pracy tylko z elementów HTML. Tylko ograniczona liczba jego funkcji będzie działać na elementach SVG. –