繁体   English   中英

奇怪的SVG / JavaScript行为

[英]Strange SVG / javascript behavior

我可能在这里缺少一些重要的东西。

我正在使用此功能更改某些SVG文本元素中的日期格式。

jQuery.fn.fixDateformat = function(){
        ar = $(this).text().split("/")
        output = [ar[1], ar[0], ar[2]].join(".")
        $(this).text(output)
    };

在控制台中,我得到了要更改的元素的数组。

> array = $("text[x='0']")
[
<text y=​"5" dy=​".71em" text-anchor=​"middle" x=​"0">​04/21/13​</text>​, 
<text y=​"5" dy=​".71em" text-anchor=​"middle" x=​"0">​04/26/13​</text>​, 
<text y=​"5" dy=​".71em" text-anchor=​"middle" x=​"0">​05/02/13​</text>​, 
<text y=​"5" dy=​".71em" text-anchor=​"middle" x=​"0">​05/08/13​</text>​, 
<text y=​"5" dy=​".71em" text-anchor=​"middle" x=​"0">​05/14/13​</text>​
]

当我将函数传递给其中一个元素时,它就会起作用。 Yeay!

> array.first().fixDateformatOnCharts()

但是,当我遍历数组时出现此错误。

> array.each(function(i,v){ v.fixDateformatOnCharts()})
TypeError: Object #<SVGTextElement> has no method 'fixDateformatOnCharts'

有任何想法吗?

您应该使用$(v)将元素v转换为jQuery对象。

array.each(function(i,v){ $(v).fixDateformatOnCharts(); });

也许最好将此功能添加到插件本身中:

jQuery.fn.fixDateformat = function() {
  return this.each(function(i, el) {
    ar = $(el).text().split("/");
    output = [ar[1], ar[0], ar[2]].join(".");
    $(this).text(output);
  });
};

因此,您可以使用array.fixDateformat();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM