无论如何可以访问拉斐尔 js 中的属性值

Is there anyway to access the attribute value in Raphael js?

本文关键字:js 属性 访问 无论如何      更新时间:2023-09-26

有没有办法比较文本的属性中的字体大小是否等于50?如何访问拉斐尔元素中的 attr() 值?例如,我有一个paper.set()里面包含很多paper.text(),我想知道是否有一个文本元素通过使用for循环获得"font-size"等于50。

就像使用 Element.attr(<attrName>, <attrValue>) 设置属性值一样,只需使用 Element.attr(<attrName>) 来检索值。请注意,<attrValue>会以文本的形式返回给您,即"50"而不是50

以下代码片段演示了这一点,尽管它可能在某些浏览器(例如 Chrome)中不起作用,可能是因为该代码段试图访问外部第三方库,即 Raphael.js。因此,要运行它,请使用Firefox或复制它并在您自己的计算机上运行它。

window.onload = function() {
  var paper = Raphael("holder", 200, 80);
  paper.set().push(
    paper.text(120, 20, "Hello").attr({"font-size": "40"}), // set attribute
    paper.text(120, 60, "Hello").attr({"font-size": "50"})
  ).items.forEach(function(item, itemNum) {
    document.getElementsByTagName('body')[0].appendChild(document.createElement("p")).innerHTML =
      "item #" + itemNum + ": " + (item.attr("font-size") === "50"); // get attribute
  });
};
<script src="http://github.com/DmitryBaranovskiy/raphael/raw/master/raphael-min.js"></script>
<div id="holder"></div>