本文介绍了jQuery生成唯一的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我所拥有的:
<a href="<?=$arItem["LINK"]?>"><?=$arItem["TEXT"]?></a>
这是一个PHP数组,其中包含一些链接.我需要在每个链接的末尾添加一个额外的哈希参数#nav-link
.
It's a PHP array, which contains some links. I need to add an extra hash parameter #nav-link
to the end of each link.
这是我尝试的方法:
<a id="likeLink" href=""><?=$arItem["TEXT"]?></a>
<script>
$(document).ready(function () {
$("#likeLink").attr("href", <?=$arItem["LINK"]?> + "#nav-link");
});
</script>
但是此代码不起作用,因为jQuery无法知道我链接到的链接.因此,我想我需要生成唯一的ids
,但不知道该怎么做.
But this code doesn't work because jQuery will not know which links I am linking to. So I guess that I need to generate the unique ids
, but don't know how to do it.
推荐答案
要生成随机ID,您可以使用此ID.
For generating random ids you could use this one.
<script type="text/javascript">
function Generator() {};
Generator.prototype.rand = Math.floor(Math.random() * 26) + Date.now();
Generator.prototype.getId = function() {
return this.rand++;
};
var idGen =new Generator();
</script>
</html>
<body>
<!-- Place this in the body of the page content -->
<button onclick="console.log(idGen.getId())">click</button>
</body>
这篇关于jQuery生成唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!