简体   繁体   English

在javascript中动态创建元素和类实现

[英]Dynamic creation of element and class implementation in javascript

I have problem with dynamic creation of element in javascript with innerHTML. 我在使用innerHTML在javascript中动态创建元素时遇到问题。 Here is the code: 这是代码:

var newElement  = document.createElement("tr");
newElement.innerHTML = ['<td><a href="javascript:void(0);">Test Suite</a></td><td>4,977</td><td class="text-align-center">',
                      '<div class="sparkline txt-color-blue text-align-center" data-sparkline-height="22px" data-sparkline-width="90px" data-sparkline-barwidth="2">2700, 3631, 2471, 1300, 1877, 2500, 2577, 2700, 3631, 2471, 2000, 2100, 3000</div>',
            ].join('\n');

$("#locationsGraph").append(newElement);

The problem is that the Element is created just right, but the element class is not implemented. 问题在于,恰好创建了Element,但是未实现element类。

With this part: 通过这一部分:

class="sparkline txt-color-blue text-align-center" data-sparkline-height="22px" data-sparkline-width="90px" data-sparkline-barwidth="2"

I should get a bar graph, but instead I get the same list of numbers. 我应该得到一个条形图,但我得到的是相同的数字列表。

Is it something with the implementation of the class in innerHTML or what? 在innerHTML中实现类有什么用吗? I also tried manually creating all elements and assigning className but the result was the same 我也尝试手动创建所有元素并分配className,但结果是相同的

根据文档 ,您需要运行$(newElement).find('div').sparkline()

You dont have a closing to match your 您没有关闭时间来匹配您的

newElement.innerHTML = ['<td><a href="javascript:void(0);">Test Suite</a></td><td>4,977</td><td class="text-align-center">',
                      '<div class="sparkline txt-color-blue text-align-center" data-sparkline-height="22px" data-sparkline-width="90px" data-sparkline-barwidth="2">2700, 3631, 2471, 1300, 1877, 2500, 2577, 2700, 3631, 2471, 2000, 2100, 3000</div>',
          '</td>' //this is missing
  ].join('\n');

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM