javascript - 如何定义 .append() 创建的元素的宽度和 ID?
问题描述
我正在尝试创建一个select
具有width="347px"
和的元素id="select-box-1"
,该元素是从中创建的.append("select")
,我已经尝试搜索.append()
但解释导致我尝试失败。
失败的尝试:
.append("<select id='select-box-1' style='width: 347px'></select>")
我的脚本的原始部分创建.append()
:
<script src="http://d3js.org/d3.v3.js"></script>
<script id="script-da-caixa-de-selecao-suspensa-1">
function caixasuspensa1(error, data) {
var select = d3.select("#caixa-suspensa-1")
.append("select")
注意:此功能适用于D3.js
完整的脚本以防测试:
<script src="http://d3js.org/d3.v3.js"></script>
<script id="script-da-caixa-de-selecao-suspensa-1">
function caixasuspensa1(error, data) {
var select = d3.select("#caixa-suspensa-1")
.append("select")
select
.on("change", function(d) {
var value = d3.select(this).property("value");
document.querySelector('#barra-de-texto-para-radar-1').value = value;
document.getElementById('botao-do-radar-1').click();
});
select.selectAll("option")
.data(data)
.enter()
.append("option")
.attr("value", function (d) { return d.value; })
.text(function (d) { return d.label; });
}
d3.csv("Lista_de_Jogos.csv", function(error, data){caixasuspensa1(error, data)});
</script>
解决方案
您可以尝试附加到innerHTML
:
document.body.innerHTML+="<select id='select-box-1' style='width: 347px'></select>";
document.body.insertAdjacentHTML("afterend", "<select id='select-box-1' style='width: 347px'></select>");
推荐阅读
- java - 使用 rmi 时出现 java.net.ConnectException
- java - org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; 糟糕的 SQL 语法
- c++ - lambda 中的 [capture] 是什么?
- r - 从数据框中行的每个元素中减去行的最小值,
- android - 在android studio中并排放置的两个垂直线性布局之间添加图像视图的问题
- flutter - 如何从 URL 重定向回 Flutter App?
- python - 将类属性添加到 ModelForm 上的查询集字段
- azure - 每当在我的 azure 虚拟规模集中添加或删除实例时调用 azure 函数
- android - GMS 服务更新会杀死 android 设备中当前正在运行的进程/服务,这些进程/服务依赖于 GMS
- java - 我无法在 Spring Boot Authentication Provider 中保留数据