首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vivus.js -如何按类选择SVG

vivus.js -如何按类选择SVG
EN

Stack Overflow用户
提问于 2016-06-10 16:06:23
回答 3查看 2.5K关注 0票数 2

是否有办法按类选择SVG而不是ID,如下所示:

代码语言:javascript
复制
<svg class="mySVG">
    <path...>
    <path...>
    <path...>
</svg>

<script>
    new Vivus('.mySVG', {duration: 200}, myCallback);
</script>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-10 16:10:39

因为您可以使用同一个类拥有多个元素。

代码语言:javascript
复制
  var els= document.getElementsByClassName("mySVG");

  for (var i = els.length - 1; i >= 0; i--) {
    new Vivus(els[i], {duration: 200}, myCallback);
  }
票数 2
EN

Stack Overflow用户

发布于 2016-06-10 16:09:24

代码语言:javascript
复制
document.getElementsByClassName('class_name');
document.querySelector('.class_name');
document.querySelectorAll('.class_name')[0];
// if that's the first SVG element with the given class that you're interested in.
// otherwise just loop through the HTMLCollection it returns
票数 0
EN

Stack Overflow用户

发布于 2016-06-10 16:23:26

我以前没用过这个库。虽然,我读了文档的来源。我觉得你不用上课。根据vivus.js的源代码。

代码语言:javascript
复制
* Check and set the element in the instance
* The method will not return anything, but will throw an
* error if the parameter is invalid
*
* @param {DOM|String}   element  SVG Dom element or id of it
*/
Vivus.prototype.setElement = function (element, options) {
// Basic check
if (typeof element === 'undefined') {
throw new Error('Vivus [constructor]: "element" parameter is required');
}

// Set the element
if (element.constructor === String) {
element = document.getElementById(element);
if (!element) {
  throw new Error('Vivus [constructor]: "element" parameter is not related       to an existing ID');
  }
}

如果元素没有ID,它似乎会抛出错误。

参考资料:https://github.com/maxwellito/vivus/blob/master/src/vivus.js

我会尝试在这个答案之前实现一些东西,并尝试使它能够接受一个类。

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37752685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档