简体   繁体   English

有没有一种方法可以使用对象构造函数创建多个具有唯一属性的HTML元素?

[英]Is there a way to use an object constructor to create multiple HTML elements but with unique attributes?

Anyways, I think that I have been staring at code too long to be able to think straight, so any help would be appreciated. 无论如何,我认为我盯着代码太久了,无法直截了当地思考,因此,我们将不胜感激。 Below is my code that seems unnecessarily repetitive. 下面是我的代码,似乎不必要地重复了。

var P1actionContainer = document.createElement("div");
    P1actionContainer.setAttribute("id", "P1actionContainer");
    P1actionContainer.classList.add("actioncontainer");
    topbox.appendChild(P1actionContainer);

            // FOLD
        var P1fold = document.createElement("input");
            P1fold.setAttribute("id", "P1foldButton");
            P1fold.setAttribute("type", "button");
            P1fold.setAttribute("value", "FOLD");
            P1fold.classList.add("actionbuttonmargins");
            //fold.addEventListener("click", stepTenFold);
            P1actionContainer.appendChild(P1fold);

            // CALL
        var P1call = document.createElement("input");
            P1call.setAttribute("id", "P1callButton");
            P1call.setAttribute("type", "button");
            P1call.setAttribute("value", "CALL");
            P1call.classList.add("actionbuttonmargins");
            //call.addEventListener("click", stepTenCall);
            P1actionContainer.appendChild(P1call);

            // BET SLIDER
        var P1betSlider = document.createElement("input");
            P1betSlider.setAttribute("id", "P1betSlider");
            P1betSlider.setAttribute("type", "range");
            P1betSlider.setAttribute("value", "0");
            P1betSlider.setAttribute("min", blinds.L1B * 2);
            P1betSlider.setAttribute("max", P1.stack);
            P1betSlider.setAttribute("step", blinds.L1S);
            P1betSlider.classList.add("actionbuttonmargins");
            P1betSlider.addEventListener("input", P1adjustBetDisplay);
            P1actionContainer.appendChild(P1betSlider);

            // BET DISPLAY
        var P1betSliderValue = document.getElementById("P1betSlider").value;
        var P1betDisplay = document.createElement("input");
            P1betDisplay.setAttribute("id", "P1betDisplay");
            P1betDisplay.setAttribute("type", "number");
            P1betDisplay.setAttribute("value", P1betSliderValue);
            P1betDisplay.setAttribute("min", blinds.L1B * 2)
            P1betDisplay.setAttribute("max", P1.stack);
            P1betDisplay.setAttribute("step", blinds.L1S)
            P1betDisplay.setAttribute("maxlength", 4);
            P1betDisplay.classList.add("actionbuttonmargins");
            P1betDisplay.addEventListener("input", P1adjustBetSlider);
            P1actionContainer.appendChild(P1betDisplay);

            // BET
        var P1bet = document.createElement("input");
            P1bet.setAttribute("id", "betButton");
            P1bet.setAttribute("type", "button");
            P1bet.setAttribute("value", "RAISE");
            P1bet.classList.add("actionbuttonmargins");
            //bet.addEventListener("click", stepTenBet);
            P1actionContainer.appendChild(P1bet);

var P2actionContainer = document.createElement("div");
    P2actionContainer.setAttribute("id", "P2actionContainer");
    P2actionContainer.classList.add("actioncontainer");
    botbox.appendChild(P2actionContainer);

            // FOLD
        var P2fold = document.createElement("input");
            P2fold.setAttribute("id", "P2foldButton");
            P2fold.setAttribute("type", "button");
            P2fold.setAttribute("value", "FOLD");
            P2fold.classList.add("actionbuttonmargins");
            //fold.addEventListener("click", stepTenFold);
            P2actionContainer.appendChild(P2fold);

            // CALL
        var P2call = document.createElement("input");
            P2call.setAttribute("id", "P2callButton");
            P2call.setAttribute("type", "button");
            P2call.setAttribute("value", "CALL");
            P2call.classList.add("actionbuttonmargins");
            //call.addEventListener("click", stepTenCall);
            P2actionContainer.appendChild(P2call);

            // BET SLIDER
        var P2betSlider = document.createElement("input");
            P2betSlider.setAttribute("id", "P2betSlider");
            P2betSlider.setAttribute("type", "range");
            P2betSlider.setAttribute("value", "0");
            P2betSlider.setAttribute("min", blinds.L1B * 2);
            P2betSlider.setAttribute("max", P2.stack);
            P2betSlider.setAttribute("step", blinds.L1S);
            P2betSlider.classList.add("actionbuttonmargins");
            P2betSlider.addEventListener("input", P2adjustBetDisplay);
            P2actionContainer.appendChild(P2betSlider);

            // BET DISPLAY
        var P2betSliderValue = document.getElementById("P2betSlider").value;
        var P2betDisplay = document.createElement("input");
            P2betDisplay.setAttribute("id", "P2betDisplay");
            P2betDisplay.setAttribute("type", "number");
            P2betDisplay.setAttribute("value", P2betSliderValue);
            P2betDisplay.setAttribute("min", blinds.L1B * 2)
            P2betDisplay.setAttribute("max", P2.stack);
            P2betDisplay.setAttribute("step", blinds.L1S)
            P2betDisplay.setAttribute("maxlength", 4);
            P2betDisplay.classList.add("actionbuttonmargins");
            P2betDisplay.addEventListener("input", P2adjustBetSlider);
            P2actionContainer.appendChild(P2betDisplay);

            // BET
        var P2bet = document.createElement("input");
            P2bet.setAttribute("id", "betButton");
            P2bet.setAttribute("type", "button");
            P2bet.setAttribute("value", "RAISE");
            P2bet.classList.add("actionbuttonmargins");
            //bet.addEventListener("click", stepTenBet);
            P2actionContainer.appendChild(P2bet);

So, is there a way to use an object constructor to create these particular elements? 那么,有没有一种方法可以使用对象构造函数来创建这些特定元素?

I've tried the code below and similar variations of it--one of which used properties in addition to values instead of just statements, as is the case below. 我尝试了下面的代码及其类似的变体-其中一种使用了除值以外的属性,而不是仅使用语句,如下所示。

var createInput = function(elementName, a, v, aa, vv, aaa, vvv)
{
    elementName = document.createElement("input");
    elementName.setAttribute(a, v);
    elementName.setAttribute(aa, vv);
    elementName.setAttribute(aaa, vvv);
};

var P1fold = new createInput("P1fold", "id", "P1foldButton", "type", "button", "value", "FOLD");

    P1fold.classList.add("...");

However, I was getting an error to the effect of "Cannot add class to undefined". 但是,我收到了“无法将类添加到未定义”的错误消息。

The first tools in code re-use are functions and parameters. 代码重用的第一个工具是功能和参数。 When only one or two things change in your code pile, pull them out as parameters and turn the repetitive bits in functions. 当您的代码堆中只有一两个东西发生变化时,将它们作为参数拉出并旋转函数中的重复位。 This should get you started... 这应该让您开始...

function makeContainer(idPrefix, doc)
{
   var container = doc.createElement('div');
   container.setAttribute('id', idPrefix + 'container');
   container.classList.add("actioncontainer");
   //code to call functions to create buttons, etc.
   container.appendChild(makeButton(idPrefix, 'Call', doc));
   container.appendChild(makeButton(idPrefix, 'Bet', doc));
   //etc...
   return container; 
}

function makeButton(idPrefix, value, doc)
{

   var button = doc.createElement('input');
   button.setAttribute('id', idPrefix + value + 'button');
   button.setAttribute('type', 'button');
   button.setAttribute('value', value);
   button.classList.add("actionbuttonmargins");
   return button;
}
//Other functions for sliders, etc.

topbox.appendChild(makeContainer('p1', document));
topbox.appendChild(makeContainer('p2', document));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery-通过获取html元素创建数组对象,点击属性 - JQuery - create array object by taking html elements,attributes on click 有什么方法可以删除HTML元素中的角度属性? - Is there any way to remove angular attributes in HTML elements? 有没有办法创建多个 HTML 并为 webpack 中的每个 html 使用相同的包? - Is there a way to create multiple HTML and use same bundle for every html in webpack? 使用jQuery连接多个HTML元素的属性? - Concatenate attributes from multiple HTML elements with jQuery? 有没有办法为HTML元素提供唯一的ID,以便使用for循环在JavaScript中创建它们? - Is there a way to give HTML elements unique IDs for using a for loop to create them in JavaScript? 循环创建多个HTML元素 - Create multiple HTML elements with a loop 如何创建一个具有html标签所有属性的对象? - How to create an Object with all attributes of a html tag? javascript - lodash - 基于多个属性创建唯一列表 - javascript - lodash - create a unique list based on multiple attributes 为自定义元素创建自定义 HTML 属性是一种好习惯吗? - Is it good practice to create custom HTML attributes for custom elements? Jquery使用数据对象查找具有HTML5数据属性的元素 - Jquery finding elements with HTML5 data attributes, using data object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM