Javascript 函数返回 [object HTMLSelectElement] 字符串而不是
我正在尝试创建并调用一个函数,该函数构建
但是,它在屏幕上呈现文本“[object HTMLSelectElement]”,而不是我想要的 元素。
function renderSelect(selectOptions) {
var select = document.createElement("select");
for (let index = 0; index < selectOptions.length; index++) {
const element = selectOptions[index];
select.insertAdjacentHTML(
"beforeend",
`<option value="${element}">${element}</option>`
);
}
// This shows the <select> element perfectly in the console
console.log(select)
return select;
}
////////// BELOW IS THE FUNCTION CALL IN ANOTHER (WHICHEVER) PART OF THE CODE
// This renders "[object HTMLSelectElement]" on the screen instead of the <select> element
`${renderSelect(selectOptions)}`;
I´m trying to create and call a function that builds a <select>
item and returns this item to be rendered on the screen.
However, it renders the text "[object HTMLSelectElement]" on the screen instead of the <select>
element that I want. In the Chrome console, though, it renders the <select>
element perfectly.
function renderSelect(selectOptions) {
var select = document.createElement("select");
for (let index = 0; index < selectOptions.length; index++) {
const element = selectOptions[index];
select.insertAdjacentHTML(
"beforeend",
`<option value="${element}">${element}</option>`
);
}
// This shows the <select> element perfectly in the console
console.log(select)
return select;
}
////////// BELOW IS THE FUNCTION CALL IN ANOTHER (WHICHEVER) PART OF THE CODE
// This renders "[object HTMLSelectElement]" on the screen instead of the <select> element
`${renderSelect(selectOptions)}`;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论