【问题标题】:Is there anyway to access the attribute value in Raphael js?无论如何可以访问Raphael js中的属性值吗?
【发布时间】:2016-06-03 09:07:57
【问题描述】:

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

【问题讨论】:

    标签: javascript svg raphael


    【解决方案1】:

    与使用Element.attr(<attrName>, <attrValue>) 设置属性值的方式相同,只需使用Element.attr(<attrName>) 检索值即可。请注意,<attrValue> 将以文本形式返回给您,即 "50" 而不是 50

    下面的 sn-p 演示了这一点,尽管它可能在某些浏览器(例如 Chrome)中不起作用,可能是因为 sn-p 正在尝试访问外部第三方库,即 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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多