Mah*_*Ali 6 html javascript css materialize
我已经用实体化库做了一个下拉菜单。它包含项目列表。现在我想在所有项目的底部查看更多链接。该链接已成功添加,但问题是当我单击该链接下拉列表/选择被关闭时。
const select = document.querySelector("select");
const fillSelect = (newOptions) => {
const options = select.querySelectorAll("option");
options.forEach((x) => x.remove());
newOptions.forEach((opt) => {
const optionElement = document.createElement("option");
optionElement.innerHTML = opt;
select.appendChild(optionElement);
});
updateSelect();
};
const addViewMoreLink = () => {
const link = document.createElement('div');
link.innerHTML = 'View more';
link.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
return false;
})
const dropdownContent = select.parentNode.querySelector(".dropdown-content");
dropdownContent.appendChild(link);
}
const updateSelect = () => {
window.M.FormSelect.init(select, {});
}
window.onload = function(){
fillSelect(["Option 1", "Option 2", "Option 3"]);
addViewMoreLink()
}Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<select id="organization-input">
</select>Run Code Online (Sandbox Code Playgroud)
我希望当我更多地单击视图时,它应该关闭选择/下拉内容并显示新选项。添加新选项不是问题。目前的问题是即使我使用了停止它也会关闭e.stopPropagation();
小智 5
<style type="text/css">
.classic-dropdown .dropdown-content.select-dropdown {
pointer-events: none;
}
.classic-dropdown .dropdown-content.select-dropdown li {
pointer-events: all;
}
</style>
Run Code Online (Sandbox Code Playgroud)
<div class="classic-dropdown">
<select id="organization-input">
</select>
</div>
<script type="text/javascript">
const select = document.querySelector("select");
const fillSelect = (newOptions) => {
const options = select.querySelectorAll("option");
options.forEach((x) => x.remove());
newOptions.forEach((opt) => {
const optionElement = document.createElement("option");
optionElement.innerHTML = opt;
select.appendChild(optionElement);
});
updateSelect();
};
const addViewMoreLink = (e) => {
const link = document.createElement('div');
link.innerHTML = 'View more';
link.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
return false;
})
const dropdownContent = select.parentNode.querySelector(".dropdown-content");
dropdownContent.appendChild(link);
}
const updateSelect = () => {
window.M.FormSelect.init(select, {});
}
window.onload = function() {
fillSelect(["Option 1", "Option 2", "Option 3"]);
addViewMoreLink()
}
document.getElementsByClassName('dropdown-content')[0]
</script>
Run Code Online (Sandbox Code Playgroud)