Appending an DOM/SVG element with the d3 javascript library changes variable reference/scope, why? -
i noticed mixed results when append dom/svg elements existing object inline , in separate step when using d3 javascript library. if @ variable referencing original svg object changes, when object appended during creation of original object. here example:
var body = d3.select("body"); var svg = body.append("svg") .attr("width", '100%') .attr("height", '100%') var html1 = svg.append("foreignobject") .attr("x", 50) .attr("y", 25) .attr("width", 200) .attr("height", 100) .attr('id', 'fo1') .append("xhtml:div") .html("lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam. lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam. lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam. lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam."); var html2 = svg.append("foreignobject") .attr("x", 250) .attr("y", 25) .attr("width", 200) .attr("height", 100) .attr('id', 'fo2'); html2.append("xhtml:div") .html("lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam. lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam. lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam. lorem ipsum dolor sit amet, consectetur adipiscing elit. donec eu enim quam."); console.log('first fo:', html1[0]); console.log('first fo id:', d3.select('#fo1')[0]); console.log('second fo:', html2[0]);
the output looks :
first fo: array[1] 0: div length: 1 parentnode: html __proto__: array[0] first fo id: array[1] 0: foreignobject#fo1.[object svganimatedstring] length: 1 parentnode: html __proto__: array[0] second fo: array[1] 0: foreignobject#fo2.[object svganimatedstring] length: 1 parentnode: html __proto__: array[0]
after appending div element foreignobject element inline during creation variable changes foreignobject div. doing in step not chenges reference. here corresponding jsfiddle explain me , tell me how avoid ?
the result of append()
operation in d3 element have appended. can chain methods set attributes, content, etc. if wasn't case, couldn't use .html()
function set content of newly-appended div
current object still parent foreignobject
.
this behavior intentional , cannot prevent unless modify d3 source. if need save references elements, append in separate calls. is
var html1 = svg.append("foreignobject") ... .attr('id', 'fo1') .append("xhtml:div") .html(...);
would become
var html1 = svg.append("foreignobject") ... .attr('id', 'fo1'); var nested = html1.append("xhtml:div") .html(...);
Comments
Post a Comment