[英]jquery and d3: why does my page reload when I use $(“svg”).remove()?
I have a page that includes a dropdown menu to choose one of several bar charts to display. 我有一个页面,其中包含一个下拉菜单,用于选择要显示的多个条形图之一。 The page improperly reloads when the dropdown selection is changed. 更改下拉菜单时,页面无法正确加载。 I have narrowed the culprit to this line: 我把罪魁祸首缩小到这一行:
$("svg").remove();
When I comment out that line, the HTML changes without the page reloading. 当我注释掉该行时,HTML会更改,而无需重新加载页面。 But I need that line (or something similar), because I want the previous chart to go away when the new chart is selected. 但是我需要那条线(或类似的东西),因为当选择新图表时,我希望上一张图表消失。
I've also tried 我也尝试过
d3.select("svg").remove();
but the same thing happens. 但同样的事情发生了。
I've added 我已经添加
event.preventDefault();
but that doesn't help either. 但这也无济于事。
I've made a jsfiddle to show my relevant HTML and JS/d3. 我做了一个jsfiddle来展示我相关的HTML和JS / d3。
My page is here if seeing the whole thing will help. 我的页面在这里,如果看到整个事情会有所帮助。 Note how the page reloads when the dropdown selection is changed. 请注意,更改下拉菜单时页面如何重新加载。
(Update Jan. 3: I have followed the suggestions in the comments/answers below, but nothing has helped. I'm still having this problem.) (1月3日更新:我已遵循以下评论/答案中的建议,但没有任何帮助。我仍然遇到此问题。)
Nope, your page is not reloading. 不,您的页面没有重新加载。 ( If it does reload you will see a spin icon in most of the browsers title). (如果确实重新加载,您将在大多数浏览器标题中看到一个旋转图标)。 Like @Mansov told in the comment It is the margin that changes when you .remove()
and add the svg
. 就像@Mansov在评论中告诉的那样,当您.remove()
并添加svg
时,边距会发生变化。
I was having this same problem. 我遇到了同样的问题。 Whenever I refreshed my chart, I was doing a .remove()
to the entire svg reference, and then calling my build()
function again. 每当刷新图表时,我都会对整个svg参考执行.remove()
,然后再次调用build()
函数。
In effect, what was happening was the <div>
containing the SVG was collapsing to 0 height, causing the page to scroll back up to fit all the remaining content, and then when I rebuilt the chart the page would remain scrolled at the top. 实际上,发生的是包含SVG的<div>
折叠到0高度,导致页面向上滚动以适合所有剩余内容,然后当我重建图表时,页面将保持在顶部滚动。
So what I did was make a "frame" for the chart svg to sit on top of: 所以我要做的是为图表svg创建一个“框架”:
//===================================================== Initialize build
function initializeBuild() {
bilSVG = this.d3.select(rawSvg)
.append("svg");
calculateMargins();
bilSVG
.attr("width", width)
.attr("height", height)
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.style("opacity", .0)
;
}
and then created a group that held the chart: 然后创建一个保存图表的组:
//===================================================== build
function build() {
chartGroup = bilSVG
.append("g")
.attr("class","bil chartgroup")
.attr("transform", function(d) {
return "translate(" + width / 2 + "," + width / 2 + ")";
})
.attr("x", width / 2)
.attr("y", width / 2)
;
}
Then when I needed to rebuild the chart, I deleted the group, and not the whole SVG: 然后,当我需要重建图表时,我删除了该组,而不是整个SVG:
//===================================================== rebuild
function rebuild() {
chartGroup = bilSVG.select(".chartgroup");
chartGroup.remove();
build();
}
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.