循环创建不可点击的按钮

时间:2018-01-08 14:32:57

标签: javascript

我的按钮有问题。我在javascript中在网格中生成新按钮,只有那些有4个按钮(完整行)的行具有可点击按钮。为什么在不完整的行中禁用按钮?

results.innerHTML = '<div class="row">'
                    for (i = 0; i < size; i++) {
                            results.innerHTML +=
                                '<div class="col-md-3">'
                                    + button
                                +'</div>'
                    rowCount++;
                    if(rowCount % 4 == 0) {
                            results.innerHTML += 
                                '</div><div class="row">'
                    }
            }
            results.innerHTML +='</div>'

2 个答案:

答案 0 :(得分:2)

  

为什么在不完整的行中禁用按钮?

这一行

results.innerHTML = '<div class="row">'

不仅仅在div内创建results开始标记也会尝试同时隐式关闭标记即可。

最后需要使用innerHTML +=一次。 (假设button有效标记本身)

  var html = "";
  html += '<div class="row">';
  for (i = 0; i < size; i++) 
  {
       html +='<div class="col-md-3">' + button +'</div>';
       rowCount++;
       if(rowCount % 4 == 0) 
       {
           html += '</div><div class="row">';
       }
  }
  html +='</div>';
  results.innerHTML += html;

答案 1 :(得分:1)

点击处理程序是什么样的?你是按类抓住按钮吗?如果是这样,您将需要为按钮创建特定的类名。我发现使用jQuery更容易做到这一点,因为你可以在点击处理程序中使用cat combined.csv "/product/10049Y4JBT/Petite-Lever-Back-Earrings-for-Little-Girls-in-14K-Yellow-Gold-with-Blue-Topaz---High-End-Childrens-Earrings/","/product/dangle-hoop-earrings-for-girls-with-cz-and-heart-dangle-in-14k-gold/" "/product/10049Y4JBT/Petite-Lever-Back-Earrings-for-Little-Girls-in-14K-Yellow-Gold-with-Blue-Topaz---High-End-Childrens-Earrings/","/product/dangle-hoop-earrings-for-girls-with-cz-and-butterfly-dangle-in-14k-gold/" "/product/10049Y4JBT/Petite-Lever-Back-Earrings-for-Little-Girls-in-14K-Yellow-Gold-with-Blue-Topaz---High-End-Childrens-Earrings/","/product/petite-lever-back-earrings-for-little-girls-in-14k-yellow-gold-with-blue-topaz-high-end-childrens-earrings/" cat unmatched.csv ,"new-url" 来引用被点击的确切按钮,即使多个按钮具有相同的类名。

如果您正在使用jQuery:$(this)是抓取文档已加载后创建的元素所必需的。

我相信还有一种纯粹的Vanilla JS方法,也许其他人可以提供这种解决方案。

干杯!