根据时间生成唯一的html ID属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <textarea name="output" id="output20181126110146" rows="5" style="width:100%"></textarea> <input type="button" value="生成唯一化ID" onclick="setOutput()"/> <input type="button" value="生成唯一ID和对应设置方法" onclick="setOutIdFuntion()"> <script> function setOutput() { document.getElementById("output20181126110146").value="id=\""+uniqueIdByFormatDate()+"\""; copy(); } function setOutIdFuntion() { var id=uniqueIdByFormatDate(); document.getElementById("output20181126110146").value="// id=\""+id+"\""+ " onclick=\"setID"+id+"()\"" +"\nfunction setID"+id+"()\n{"+"\nvar id=document.getElementById(\""+id+"\");"+"\n}"; copy(); } function copy() { var output = document.getElementById("output20181126110146"); output.select(); document.execCommand("Copy"); if(confirm("代码已经复制到剪贴板粘贴即可")) { output.value=""; } } function uniqueIdByFormatDate() { var date1=new Date(); var str = ""; var year = date1.getFullYear(); str += year; var month = date1.getMonth() + 1; if (month < 10) str += "0"; str += month; var date = date1.getDate(); if (date < 10) str += "0"; str += date; var hour = date1.getHours() + 1; if (hour < 10) str += "0"; str += hour; var moinute = date1.getMinutes() + 1; if (moinute < 10) str += "0"; str += moinute; var second = date1.getSeconds() + 1; if (second < 10) str += "0"; str += second; return str; } </script>
|