简体   繁体   English

奇怪的SVG / JavaScript行为

[英]Strange SVG / javascript behavior

I might be missing something essential here. 我可能在这里缺少一些重要的东西。

I am changing the date format in some SVG text elements with this function. 我正在使用此功能更改某些SVG文本元素中的日期格式。

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

In the console I get an array of the elements I want to change. 在控制台中,我得到了要更改的元素的数组。

> 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>​
]

When I pass the function to one of elements it works. 当我将函数传递给其中一个元素时,它就会起作用。 Yeay! Yeay!

> array.first().fixDateformatOnCharts()

But, when I loop through the array I get this error. 但是,当我遍历数组时出现此错误。

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

Any ideas? 有任何想法吗?

You should use $(v) to convert an element v to a jQuery object. 您应该使用$(v)将元素v转换为jQuery对象。

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

Maybe it would be better to add this funcionality to the plugin itself: 也许最好将此功能添加到插件本身中:

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);
  });
};

So you can use array.fixDateformat(); 因此,您可以使用array.fixDateformat(); .

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

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