Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据阵列中对象的数量创建按钮?_Javascript_Arrays_Html_Button_Foreach - Fatal编程技术网

Javascript 如何根据阵列中对象的数量创建按钮?

Javascript 如何根据阵列中对象的数量创建按钮?,javascript,arrays,html,button,foreach,Javascript,Arrays,Html,Button,Foreach,我有一个包含3个对象的数组 map.la = [object, object, object]; 我想为每个对象创建一个按钮。我必须动态创建按钮的原因是对象的数量 时而不同 如何循环遍历每个对象并为其创建按钮,然后将按钮的工具提示设置为与每个对象内的name属性相等 按钮: this._selectionbutt = new SegButton({ items: [ //each of the below items (this.selection) should b

我有一个包含3个对象的数组

map.la = [object, object, object];
我想为每个对象创建一个按钮。我必须动态创建按钮的原因是对象的数量 时而不同

如何循环遍历每个对象并为其创建按钮,然后将按钮的工具提示设置为与每个对象内的name属性相等

按钮:

this._selectionbutt = new SegButton({
    items: [

        //each of the below items (this.selection) should be added here to items array. because we have 3 buttons there
        //should be three unique items here. the items should have the same name as the variable we instantiate the
        //button with.

        this._oSelection
    ]
});

this.selection = new SegButtItems({
    icon: "map",
    tooltip: //this should be the name property of each item
    press: this._handleSelection();
});
一旦用户选择按钮,任何按钮都将被带到相同的函数(this.\u handleSelection())和此函数 将需要检查名称属性字符串,例如“map333”,仅此而已

我们将高度赞赏任何指导:)


谢谢你这里有一个简单的例子:

HTML代码。创建div,我们将在其中添加按钮

<div id="buttons"></div>

感谢您的回复,但它必须是全部javascript,并且必须使用上面描述的“this.\u selectionbutt”。
//create objects

var Object1 = {
    icon: "map",
    tooltip: "Press Me",
    press: 'console.log("Button1 pressed")'
};
var Object2 = {
    icon: "map",
    tooltip: "Press Me",
    press: 'console.log("Button2 pressed")'
};
var Object3 = {
    icon: "map",
    tooltip: "Press Me",
    press: 'console.log("Button3 pressed")'
};

//add objects to array
var ar = [Object1, Object2, Object3];
//add buttons for every object in array
ar.forEach( function(v) { 
    var button= document.createElement('button');
    button.type= 'button';
    button.appendChild(document.createTextNode(v.tooltip));
    button.setAttribute('onClick',v.press);
  document.getElementById("buttons").appendChild(button);
 } );