[英]Getting <img> svg preserved width / height or viewBox with Javascript (JQuery)
Is there a possibility to read a preserved width
and height
or viewBox
values of the img
with svg
source file? 是否可以读取带有svg
源文件的img
的保留的width
和height
或viewBox
值? I tried something like this but still get undefined
. 我尝试了类似的方法,但仍然undefined
。
$('img.cart').load(function(){ console.log($('img.cart').children('svg').attr('viewBox')); });
img { width: 50%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img class="cart" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/cartman.svg">
By using SVG inside img tag you cannot access the svg attributes mostly try using tag attributes $('img.cart').css('height'));
通过在img标签内使用SVG,您将无法访问svg属性,大多数情况下,请尝试使用标签属性$('img.cart').css('height'));
$('img.cart').load(function(){ console.log($('img.cart').css('height')); });
img { width: 50%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img class="cart" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/cartman.svg" height='30px;'> <img class="cart" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/cartman.svg" height='130px;'> <img class="cart" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/cartman.svg">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.