- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
自从我接起d3.js以来已经有一段时间了。看来我有点生疏了。我正在尝试创建一个表单,可以通过单击 +
添加更多输入字段,并通过单击 -
删除现有输入字段。
为了尝试发现我的错误,我开始分别用绿色、黄色和红色对 enter()
、update 和 exit()
进行着色。
原始数据数组有两个元素,因此它们显示为绿色:
然后我单击加号,将一个新元素推送到数组中,我期望看到两个黄色和一个绿色,但我看到除了最后一个元素之外的所有元素都被删除,如果我单击 +,则会重复此情况再次:
再次加上:
我将我的代码与经典的 General Update Pattern 进行了比较除了我使用电子邮件设置按键的方式之外,我看不到任何重要的东西。这是我添加的代码,用于解决另一个根本问题,即并非所有框都被添加,而是每 3 个框中添加一个。
我的注释代码如下:
var renderFriends = function () {
console.log("Rendering friends:" + friendsList)
var friends = d3.select('.friends-container')
.selectAll('div')
.data(friendsList, function(d,i) {
// this was something I added when I thought the problem were the keys
return d
})
// updates will be yellow
friends.classed("update", true)
var enter = friends.enter()
// Friend box
// all the divs are because I'm using foundation css
// the new class is what marks the font green
var friendBox = enter.append('div').classed('large-12 columns new', true)
friendBox.append('div').classed('large-8 columns', true)
.append("input")
.attr("type", "text")
.attr("value", String)
// Icon box
var iconBox = friendBox.append('div').classed('large-2 left columns', true)
.append("i")
.classed('fi-minus', true)
.on("click", function(d) {
// out of scope for this question
console.log("Removing:" + d)
friendsList.remove(friendsList.indexOf(d))
renderFriends()
})
// exit state should colour the fonts red
friends.exit().classed('remove', true)
}
最佳答案
我用自定义样式做了一个小测试,这就是得到的结果(当我单击减号按钮时):
所有元素都有绿色背景,因为它们都有"new"类,“更新”元素有黄色边框,“删除”元素有红色背景。所以我注意到你有一个不同的 Div 嵌套,问题是当你执行 selectAll('div') 将选择所有 div 并且 d3 期望每个被选择的 div 元素是一个数据与其对应的元素。
因此,如果您想添加另一个元素,并且您的 FriendsList 是:
friendsList = ['a@test.com','b@test.com','c@test.com'];
d3.selectAll('div') 将采用 6 个 div(当您有 2 个 friend 并添加一个时),并且它只会绑定(bind) 3 个元素,因为您的数据集仅包含 3 个 friend ,并且它的目标是其余元素为“退出”。
要解决这个问题,只需使用“.friend”之类的类更改您的选择,并将其添加到要插入的每个元素(仅限主容器 div);像这样:
关于javascript - D3 元素被删除,尽管我只是添加新元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25712146/
我正在用 Java 编写代码,并且使用 Vaadin 8 扩展。 我有一个 Vaadin 组合盒,效果很好。但我不仅想从组合框中选择项目,还想选择书面输入。这意味着我想使用组合框作为下拉菜单以及文本编
我正在尝试将 AJAX 添加到 JQuery ListView 中并呈黄色闪烁,但我似乎无法使其正常工作。谁能指出我正确的方向? http://jsfiddle.net/zFybm/ 最佳答案 根据
我有这个样式表: .pixel{ position: absolute; height: 10px; width: 10px; background-color: #f
这是我用来将新行推送到容器的一行代码: this.$el.append(new ItemView(item).render().el); 其中item是Backbone.js model,render
我正在尝试在 anguar.js 中制作一些测试应用程序,但遇到了问题。我的 js 文件包含: live = angular.module('live',[]); live.controller('p
如何绑定(bind)页面加载后创建的新元素? 我有这样的东西 system = function() { this.hello = function() { alert
html5 新元素(页眉、导航、页脚等)在 IE 中不工作 最佳答案 您需要包含 HTML5 shiv 脚本以允许在旧版 IE 浏览器中设置 HTML5 元素的样式:http://code.googl
我是一名优秀的程序员,十分优秀!