簡體   English   中英

如何在使用Javascript創建按鈕時包含變量?

[英]How to include variable when creating button with Javascript?

我通過添加此代碼從javascript for循環動態創建按鈕。

var container = document.getElementById('myList');
var new_element = document.createElement('li');
new_element.innerHTML = '<button type="button" id="">label</button>';

我需要將數組中的元素添加到按鈕id和label中。 像這樣的例子;

arrMarkers[i].title

如何將這些數組值包含在創建按鈕的字符串中?

newElement.innerHTML = '<button type="button" id="' + arrMarkers[i].id + '">' + arrMarkers[i].title + '</button>';

據我所知,你正在尋找字符串連接:

new_element.innerHTML = '<button type="button" id="' + arrMarkers[i].id + '">' + arrMarkers[i].title + '</button>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM