我正在尝试在 div 中附加一个 div 并在其中加载 JS。我实际上想做 this link 正在做的事情,但这里的 div 已经预定义,并且 JS 和 CSS 文件正在应用到该 div 之上,但我需要附加一个 div 广告,然后将其用作里程表。我该怎么做?
这就是我正在尝试的。
<div id="abc" class="odometer">13</div>
$("#abc").append("<div id = 'odometer' class = 'odometer'><script src='http://github.hubspot.com/odometer/odometer.js'></script> <link rel='stylesheet' href='http://github.hubspot.com/odometer/themes/odometer-theme-car.css' />")
abc.innerHTML = 222456.89;
我没有得到所需的 O/P。我怎样才能做到这一点?
最佳答案
以下内容写在文档中:
How To Use.
Add the js and a theme file to your page:
Any element with class name "odometer" will automatically be made into an Odometer! When you want to >update the value, simply update it the same way you normally would.
element.innerHTML = 123 // Native, or. $('.odometer').html(123) // with jQuery
因此,只需创建您选择的 HTML 结构并将 .odometer 类添加到您想要在其中渲染里程表的 div 中,它就会自动执行此操作,因为这些人将 .odometer 作为过滤器来查找源节点。
您实际上不需要将脚本包含在 div 中,将其包含在 body 中的任何位置,就可以开始了。
为了更加清晰,我添加了边框
.odometer {
font-size: 100px;
border: solid 4px green;
}
.outer {
border: solid 4px red;
}
关于javascript - 在div内添加一个div并加载js文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19901847/