簡體   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