1

i am creating svg chart which contains so many elements like rectangle,path,circle,line etc.. am creating the element by passing json options as well as element where am going to append refer below code.

 this.svgLink = "http://www.w3.org/2000/svg";


drawPath: function (options, element) {
        var path = document.createElementNS(this.svgLink, "path");
        $(path).attr(options).appendTo(element);

    },

    drawLine: function (options, element) {
        var path = document.createElementNS(this.svgLink, "line");
        $(path).attr(options);
        $(path).appendTo(element);

    },

    drawCircle: function (options, element) {
        var circle = document.createElementNS(this.svgLink, "circle");
        $(circle).attr(options).appendTo(element);
    },
    drawEllipse: function (options, element) {
        var ellipse = document.createElementNS(this.svgLink, "ellipse");
        $(ellipse).attr(options).appendTo(element);
    },

am passing the options like below code.

var rectOptions = {
            'id': this.svgObject.id +'_ZoomArea', 'x': x, 'y': y, 'width': width, 'height': height,
            'fill': 'rgba(69,114,167,0.25)', 'stroke-width': 1, 'stroke':'rgba(69,114,167,0.25)','clip-path': 'url(#' + this.svgObject.id +'_ChartAreaClipRect)'
        };

now i want to pass some other options for particular element which was created already instead of creating again just want to replace the attribute values with new values just like $.extend in jquery.

how can i replace the attributes values with new values ?

Thanks,

Siva

4

1 回答 1

1

您可以使用$(e).attr(rectOptions)新属性覆盖现有属性。

不过,让我有点担心的一件事是:您真的不应该更改id选项。这可能会混淆保存旧 ID 以更新元素的代码。

相反,您可能想要使用以下代码:

var id = '#' + this.svgObject.id +'_ZoomArea';
var rectOptions = {
            'x': x, 'y': y, 'width': width, 'height': height,
            'fill': 'rgba(69,114,167,0.25)', 'stroke-width': 1, 'stroke':'rgba(69,114,167,0.25)','clip-path': 'url(#' + this.svgObject.id +'_ChartAreaClipRect)'
        };
$(id).attr(rectOptions);
于 2013-05-16T12:46:46.093 回答