使用javascript的唯一随机DIV ID [英] Unique Random DIV ID using javascript

查看:112
本文介绍了使用javascript的唯一随机DIV ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个使唯一随机DIV ID的JavaScript。目前我正在使用它与PHP






 <?php 
函数getRandomWord($ len = 10){
$ word = array_merge(range('a','z'),range('A','Z'));
shuffle($ word);
return substr(implode($ word),0,$ len);
}
$ divid = getRandomWord();
$ classid = getRandomWord();
?>

div =<?php echo $ divid; ?>
class =<?php echo $ classid; ?>

< div id =<?php echo $ divid;?> class =<?php echo $ classid;?>> < / DIV>






以上PHP代码工作正常, DIV的唯一ID Randomly,

如何做与javascript
相同的谢谢。

解决方案

这是一个工作片段中的简单随机字符串生成器,因此您可以看到它生成的内容:



  function makeRandomStr(len){var chars =abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789; var result =,rand; for(var i = 0; i< len; i ++){rand = Math.floor(Math.random()* chars.length); result + = chars.charAt(rand); } return result;} document.getElementById(run)。addEventListener(click,function(){var div = document.createElement(div); div.innerHTML = makeRandomStr(40); document.getElementById(结果)。appendChild(div);});  

  < button id =run> Make Random< / code>< / $ p>< br>< br>< pre id =results>< 

确保世界上独一无二的唯一方法是拥有一台中央服务器,用于跟踪以前的所有数据分配的字符串,因此不能重复使用。但由于你没有说明它需要多么独特,我提供了一个随机字符串。如果延长时间,冲突几率会接近零。




您还可以添加其他环境元素,例如时间戳,鼠标位置等等,例如,在这里一个时间戳被添加到字符串的末尾,所以你不仅会不小心产生相同的随机字符串,而且你'd也必须在同一时间点完成。

  function makeRandomStr(len){var chars =abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789; var result =,rand; for(var i = 0; i< len; i ++){rand = Math.floor(Math.random()* chars.length); result + = chars.charAt(rand); } return result +_+ Date.now();} document.getElementById(run)。addEventListener(click,function(){var div = document.createElement(div); div.innerHTML = makeRandomStr(40); document.getElementById(results)。appendChild(div);});  

 


I need a javascript which make Unique Random DIV ID. Currently i am using it with PHP


<?php 
function getRandomWord($len = 10) {
    $word = array_merge(range('a', 'z'), range('A', 'Z'));
    shuffle($word);
    return substr(implode($word), 0, $len);
}
$divid = getRandomWord();
$classid = getRandomWord();
?>

div = <?php echo  $divid; ?>
class = <?php echo  $classid; ?>

<div id="<?php echo  $divid; ?>" class="<?php echo  $classid; ?>"> </div>


The Above PHP code are working fine and provide a unique ID for DIV Randomly,

How to do same with javascript Thank You.

解决方案

Here's a simple random string generator in a working snippet so you can see what it generates:

function makeRandomStr(len) {
  var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var result = "", rand;
  for (var i = 0; i < len; i++) {
    rand = Math.floor(Math.random() * chars.length);
    result += chars.charAt(rand);
  }
  return result;
}

document.getElementById("run").addEventListener("click", function() {
  var div = document.createElement("div");
  div.innerHTML = makeRandomStr(40);
  document.getElementById("results").appendChild(div);
});

<button id="run">Make Random</button><br><br>
<pre id="results"></pre>

The ONLY way to guarantee perfectly unique in the world would be to have a central server that was keeping track of all previously allocated strings so no duplicate could ever be used. But since you did not specify how unique it needed to be, I offered a random string. If you make it longer, your odds of conflict go to near zero.


You can also add other environmental elements such as timestamp, mouse positions, etc... that make it even less likely to ever conflict.

For example, here a timestamp is added to the end of the string so you'd not only have to accidentally generate the same random string, but you'd also have to do it at exactly the same clock time.

function makeRandomStr(len) {
  var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var result = "", rand;
  for (var i = 0; i < len; i++) {
    rand = Math.floor(Math.random() * chars.length);
    result += chars.charAt(rand);
  }
  return result + "_" + Date.now();
}

document.getElementById("run").addEventListener("click", function() {
  var div = document.createElement("div");
  div.innerHTML = makeRandomStr(40);
  document.getElementById("results").appendChild(div);
});

<button id="run">Make Random</button><br><br>
<pre id="results"></pre>

这篇关于使用javascript的唯一随机DIV ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆