如何检查svg是否具有填充不透明度与填充不透明度为0?

时间:2015-08-28 22:45:04

标签: javascript jquery svg

假设我想检查SVG元素的填充不透明度。我这样做

elem.attr('fill-opacity');

如果元素没有填充不透明度属性或者填充不透明度已设置为0,则可以返回0。

我如何区分这两种情况?

2 个答案:

答案 0 :(得分:2)

难道你不能简单地使用elem.hasAttribute?

答案 1 :(得分:1)

如果未指定fill-opacity,则attr()会返回undefined

alert("fill-opacity (DOM) ="+document.getElementById("test").getAttribute("fill-opacity"));
alert("fill-opacity (jQuery) ="+$("#test").attr("fill-opacity"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg>
  <rect id="test" width="10" height="10"/>
</svg>

相关问题