d3.js – 设置D3中元素的值?

前端之家收集整理的这篇文章主要介绍了d3.js – 设置D3中元素的值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何设置< select>的值?元素,使用D3.js?

我正在尝试这个:

d3.select('#myselect').attr('value','France');

但它没有用.

JSFiddle:http://jsfiddle.net/06z9tnzs/

解决方法

您需要使用property而不是attr来设置值.

d3.select(‘#myselect’).property(‘value’,’France’);

来自d3文档:

Some HTML elements have special properties that are not addressable using standard attributes or styles. For example,form text fields have a value string property,and checkBoxes have a checked boolean property. You can use the property operator to get or set these properties,or any other addressable field on the underlying element,such as className.

https://github.com/mbostock/d3/wiki/Selections#property

关于你的JSFiddle缺少d3.js引用而没有在select元素上指定“myselect”的id,Albert的注释也是正确的.

原文链接:https://www.f2er.com/js/156262.html

猜你在找的JavaScript相关文章