【问题标题】:Is there a way to use an object constructor to create multiple HTML elements but with unique attributes?有没有办法使用对象构造函数来创建多个具有唯一属性的 HTML 元素?
【发布时间】:2016-10-04 04:30:47
【问题描述】:

无论如何,我认为我一直盯着代码太久无法思考,所以任何帮助都将不胜感激。下面是我的代码,似乎不必要地重复。

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);

那么,有没有办法使用对象构造函数来创建这些特定元素?

我已经尝试过下面的代码及其类似的变体——其中一个除了值之外还使用了属性,而不仅仅是语句,如下例所示。

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("...");

但是,我收到了“无法将类添加到未定义”的错误。

【问题讨论】:

  • 创建 DOM 元素当然可以在构造函数中完成,但这两个概念彼此之间没有任何特别的共同点。 JavaScript 中使用构造函数来创建特定类型的 JavaScript 元素;它与 DOM 没有任何关系。
  • @Pointy 特定种类的 JS 元素,例如 Object.prototypes?或者你指的是哪些元素?但是我的代码与 DOM 及其节点交互。事实上,上面的代码创建了 HTML 元素并将它们插入到 DOM 中。那么有没有办法使用对象构造函数来创建具有独特属性的独特 HTML 元素?
  • 对不起;我应该使用“对象”这个词——JavaScript objects 就是我的意思。
  • 您的评论中对这个问题的回答是“不是真的”。 DOM 元素是浏览器中 JavaScript 运行时本机环境的一部分,但它们不是 JavaScript,也不属于语言本身。
  • @Pointy 所以上面的代码虽然冗长,但不是完全不必要的冗长吗?

标签: javascript html oop object


【解决方案1】:

代码重用的第一个工具是函数和参数。当您的代码堆中只有一两件事情发生变化时,将它们作为参数拉出来,并转换函数中的重复位。这应该让你开始......

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));

【讨论】:

  • 我已经尝试过下面的代码和它的变体(其中一个使用了除了值之外的属性,而不仅仅是语句,如下例所示)。 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("...");
  • 但是,我收到了“无法将类添加到未定义”的错误。我不确定您的代码块与我的代码块有何不同?你的效果如何?你能达到我正在寻找的效果并将其发布在某个地方吗? @dbugger
猜你喜欢
  • 2018-02-06
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多