我有这个生成随机字符串的函数!
function randString() {
var char = "0123456789abcdefghijklmnopqrstuvwxyz",
fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
genHash = "",
i;
for (i = 0; i < 8; i++) {
var rnum = Math.floor(Math.random() * char.length)
return genHash += char.substring(rnum, rnum + 1)
}
}
我尝试做的是当我点击一个按钮时,我希望genHash
成为各种类名。
$("[data-action=newlist]").on("click", function() {
randString()
var newBtnList = $("<em>", {
text: genHash,
title: genHash,
class: "new-"+ genHash
}).appendTo("[data-action=list-tree]").on("click", function() {
// Show active editor
$("." + genHash).removeClass("hide")
})
})
我知道我可以在randString()
函数中执行此操作(例如将其设置为输入值并从输入中获取值),但我想要计算的是out是如何在函数外部进行的,从而将函数中的字符串作为标题中的状态返回。
function randString() {
var char = "0123456789abcdefghijklmnopqrstuvwxyz",
fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
genHash = "",
i;
for (i = 0; i < 8; i++) {
var rnum = Math.floor(Math.random() * char.length)
return genHash += char.substring(rnum, rnum + 1)
}
}
// Create a new list
$("[data-action=newlist]").on("click", function() {
randString()
var newBtnList = $("<em>", {
text: genHash,
title: genHash,
class: "new-"+ genHash
}).appendTo("[data-action=list-tree]").on("click", function() {
// Show active editor
$("." + genHash).removeClass("hide")
})
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" data-action="newlist">
New
</button>
<div data-action="list-tree"></div>
&#13;
答案 0 :(得分:2)
由于randString()
循环中的return
语句,您的for
函数仅返回第一个随机字符。在 for
:
function randString() {
var char = "0123456789abcdefghijklmnopqrstuvwxyz",
fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
genHash = "",
i;
for (i = 0; i < 8; i++) {
var rnum = Math.floor(Math.random() * char.length)
genHash += char.substring(rnum, rnum + 1)
}
return genHash;
}
然后,您可以将返回的字符串分配给Click事件处理程序中的变量:
var genHash = randString();
另请注意,您可以在第二个单击处理程序中使用this
关键字来引用元素,而不是从genHash
字符串构建选择器。试试这个:
$("[data-action=newlist]").on("click", function() {
var genHash = randString();
var newBtnList = $("<em>", {
text: genHash,
title: genHash,
class: "new-" + genHash + ' hide'
}).appendTo("[data-action=list-tree]").on("click", function() {
$(this).removeClass("hide")
})
});
答案 1 :(得分:1)
尝试将功能的主循环更改为
Import-Csv users.csv |
select Surname, @{n='GivenName';e={$_.'FirstName'}},
@{n='samaccountname';e={$e = FirstName.Substring(0,2) Surname}}
然后更改字符串
for (i = 0; i < 8; i++) {
var rnum = Math.floor(Math.random() * char.length)
genHash += char.substring(rnum, rnum + 1)
}
return genHash;
在您的代码中
randString()
而且,是的,在课程定义中删除“new-”。或者用“.new - ”+ genHash
替换“。”+ genHash答案 2 :(得分:1)
您正尝试在genHash
内返回loop
变量值,这是不正确的。只需在循环后返回genHash
变量值。
尝试这个
function randString() {
var char = "0123456789abcdefghijklmnopqrstuvwxyz",
fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
genHash = "",
i;
for (i = 0; i < 8; i++) {
var rnum = Math.floor(Math.random() * char.length)
genHash += char.substring(rnum, rnum + 1)
}
return genHash ; //change here
}