javascript - 如何调整下拉菜单的高度
问题描述
解决方案
问题是您在单击下拉菜单时加载项目。在项目加载之前,您已经展开下拉列表。如果您单击它并重新打开它,它将是正确的大小。解决方案是在单击下拉列表之前加载项目。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script>
var data = JSON.parse("[{\"name\":\"EE&CS\",\"id\":1},{\"name\":\"Management\",\"id\":2},{\"name\":\"Subjects\",\"id\":3},{\"name\":\"Other\",\"id\":32},{\"name\":\"Test\",\"id\":33}]");
window.onload = function () {
var select = document.getElementById("select-test");
for (var i = 0; i < data.length; i++) {
var subject = data[i];
select.appendChild(getNode("<option value=\"" + subject.id + "\">" + subject.name + "</option>"));
}
};
function getNode(str) {
return new DOMParser().parseFromString(str, "text/html").body.childNodes[0];
}
</script>
</head>
<body>
<select id="select-test" data-loaded="0">
<option value="" disabled selected>-- Choose subject --</option>
</select>
</body>
</html>
推荐阅读
- c - 二进制操作数无效!=(具有结构和 void *)
- node.js - NodeJS 路由是相对的还是根相对的,具体取决于用户代理
- bash - bash 从另一个脚本运行 curl
- flutter - 如何将 qrcode 导出为带有颤振的图像 png
- javascript - 如何从 HTML 中调用 javascript 对象方法
- css - 当它进入新行时,如何在整个屏幕宽度上拉伸最后一个网格项目?
- unity3d - Unity UI 屏幕尺寸问题
- android - 更新 Android Studio 停止颤振框架
- django - 我在我的谷歌驱动器中搜索了我的媒体文件。我可以在 Django 中获取我的媒体文件吗?
- python - 带有 XML 列和多个值的 Pyspark 数据框:从中提取列