我想为我需要保存在数据库中的每个html生成一个随机数据id,但我不知道如何正确实现它
var html='<div class="portlet portlet-sortable light bordered ui-widget-content ui-resizable" style="display: block;" data-id:Generator()>'
JS
function Generator(){};
Generator.prototype.rand = Math.floor(Math.random() * 26) + Date.now();
Generator.prototype.getId = function() {
return this.rand++;
完整的HTML
idGen = new Generator();
var $randomId = idGen.getId();
var html='<div class="portlet portlet-sortable light bordered ui-widget-content ui-resizable" style="display: block;" data-id="' + $randomId + '">'
+'<div class="portlet-title ui-sortable-handle">'
+'<div class="caption font-green-sharp">'
+'<i class="icon-speech font-green-sharp"></i>'
+'<span class="caption-subject bold uppercase"> '+$widgetTitle+'</span>'
+'</div>'
+'<div class="actions">'
+'<a href="javascript:;" class="btn btn-circle btn-default btn-sm remove">'
+'<i class="fa fa-times"></i> Remove </a>'
+'<div class="btn-group">'
+'<a class="btn btn-circle btn-default " href="javascript:;" data-toggle="dropdown" aria-expanded="false">'
+'<i class="fa fa-wrench"></i> Settings'
+'<i class="fa fa-angle-down"></i></a>'
+'<ul class="dropdown-menu pull-right">'
+'<li>'
+'<a href="javascript:;"><i class="fa fa-pencil"></i> Edit </a></li>'
+'<li><a href="javascript:;"><i class="fa fa-trash-o"></i><span class="delete"> Delete </span></a></li>'
+'<li><a href="javascript:;"><i class="fa fa-ban"></i> Ban </a></li>'
+'<li class="divider"> </li>'
+'<li><a href="javascript:;"> Make admin </a></li>'
+'</ul>'
+'</div>'
+'<a class="btn btn-circle btn-icon-only btn-default fullscreen" href="javascript:;"></a>'
+'</div>'
+'</div>'
+'<div class="portlet-body">'
+'<div>'+$widgetContent+'</div>'
+'</div>'
+'</div>';
return html;
},
答案 0 :(得分:1)
您需要连接字符串部分中的值:
console.log('I work fine');
console.log = 'I screw things up!';
console.log('I cannot log to the console'); // every `console.log`s below here do not work
或者从html字符串创建jQuery对象并执行:
var html='<div class="..." style="..." data-id="' + Generator() +'">'
答案 1 :(得分:0)
Javascript-使用setAttribute对数据号进行DOM操作
let getRandomNumber = Math.floor(Math.random() * 6) + 1;
let myRandom = document.getElementById("my-random");
myRandom.setAttribute("data-number", getRandomNumber )
@import url('https://fonts.googleapis.com/css?family=Roboto');
.ball {
display: inline-block;
width: 100%;
height: 100%;
margin: 0;
border-radius: 50%;
position: relative;
background: radial-gradient(circle at 50% 40%, #fcfcfc, #efeff1 66%, #9b5050 100%);
overflow: hidden;
animation: ballGrow 2s ease-out forwards;
transform: scale(0.5);
}
.ball:after {
content: "";
position: absolute;
top: 5%;
left: 10%;
width: 100%;
height: 100%;
border-radius: 50%;
background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.8) 14%, rgba(255, 255, 255, 0) 24%);
-webkit-transform: translateX(-80px) translateY(-90px) skewX(-20deg);
-moz-transform: translateX(-80px) translateY(-90px) skewX(-20deg);
-ms-transform: translateX(-80px) translateY(-90px) skewX(-20deg);
-o-transform: translateX(-80px) translateY(-90px) skewX(-20deg);
transform: translateX(-80px) translateY(-90px) skewX(-20deg);
}
.stage {
width: 300px;
height: 300px;
display: inline-block;
margin: 20px;
-webkit-perspective: 1200px;
-moz-perspective: 1200px;
-ms-perspective: 1200px;
-o-perspective: 1200px;
perspective: 1200px;
-webkit-perspective-origin: 50% 50%;
-moz-perspective-origin: 50% 50%;
-ms-perspective-origin: 50% 50%;
-o-perspective-origin: 50% 50%;
perspective-origin: 50% 50%;
}
.number {
position: absolute;
width: 100%;
text-align: center;
line-height: 300px;
font-size: 160px;
color: blue;
font-family: 'Roboto', sans-serif;
animation: ballRoll 2s ease-out;
}
.number:after {
content: attr(data-number);
position: absolute;
transform: translateX(-75%);
opacity: 0;
animation: numberReveal 0.1s 1.5s reverse forwards;
}
.number:before {
content: '?';
position: absolute;
transform: translateX(-25%);
animation: numberReveal 0.1s 1.5s forwards;
}
@keyframes ballRoll {
0%, 20%, 50% {
opacity: 1;
transform: translateY(0) rotateX(0) scale(1);
}
10%, 35%, 75% {
transform: translateY(100%) rotateX(170deg) scale(0.4);
}
11%, 36%, 76% {
transform: translateY(-100%) rotateX(-170deg) scale(0.3);
opacity: 0;
}
}
@keyframes ballGrow {
0% {
transform: scale(0.5);
}
100% {
transform: scale(1);
}
}
@keyframes numberReveal {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
.stage:nth-child(2) .ball,
.stage:nth-child(2) .number {
animation-delay: 0.3s;
}
.stage:nth-child(2) .number:after,
.stage:nth-child(2) .number:before {
animation-delay: 1.8s;
}
.stage:nth-child(3) .ball,
.stage:nth-child(3) .number {
animation-delay: 0.6s;
}
.stage:nth-child(3) .number:after,
.stage:nth-child(3) .number:before {
animation-delay: 2.1s;
}
.stage:nth-child(4) .ball,
.stage:nth-child(4) .number {
animation-delay: 0.9s;
}
.stage:nth-child(4) .number:after,
.stage:nth-child(4) .number:before {
animation-delay: 2.4s;
}
<section class="stage">
<figure class="ball">
<span class="number" id="my-random" data-number="?"> </span>
</figure>
</section>