[.forEach.call()在JavaScript中做什么?(What does [].forEach.call() do in JavaScript?)
我正在看一些代码片段,我发现多个元素通过一个forEach应用于一个空数组来调用一个节点列表的函数。
例如我有一些像:
[].forEach.call( document.querySelectorAll('a'), function(el) { // whatever with the current node });
但我不明白它是如何工作的。 任何人都可以解释我在forEach前面的空阵列的行为以及
call
工作原理?I was looking at some snippets of code, and I found multiple elements calling a function over a node list with a forEach applied to an empty array.
For example I have something like:
[].forEach.call( document.querySelectorAll('a'), function(el) { // whatever with the current node });
but I can't understand how it works. Can anyone explain me the behaviour of the empty array in front of the forEach and how the
call
works?
原文:https://stackoverflow.com/questions/16053357
最满意答案
看起来我想出来了
/[^\d,.]/g
所以功能是
var cleanInputOut = function (element) { element.value = element.value.replace(/[^\d,.]/g, '') }
我真的不明白为什么^字符既用于标记否定集和行的开头,但它似乎正在工作。
我之前尝试过这个正则表达式并且它不时表现得很奇怪,但我不确定问题出在正则表达式本身。 无论如何,欢迎任何关于方法或正则表达式的建议。
It looks like I figured it out using
/[^\d,.]/g
So the function is
var cleanInputOut = function (element) { element.value = element.value.replace(/[^\d,.]/g, '') }
I don't really get why the ^ character is used both to mark a negated set and the beginning of the line, but it appears to be working.
I tried this regex before and it behaved quite strangely from time to time but I am not sure the issue was with the regex itself. Anyway, any advices as to the approach or regex are welcome.
相关问答
更多-
看起来我想出来了 /[^\d,.]/g 所以功能是 var cleanInputOut = function (element) { element.value = element.value.replace(/[^\d,.]/g, '') } 我真的不明白为什么^字符既用于标记否定集和行的开头,但它似乎正在工作。 我之前尝试过这个正则表达式并且它不时表现得很奇怪,但我不确定问题出在正则表达式本身。 无论如何,欢迎任何关于方法或正则表达式的建议。 It looks like I figur ...
-
用点Char替换逗号(Replacing comma with dot Char)[2023-04-21]
当您获取但无效时,则返回空字符串。 你可以这样做来检查: $("input[type=number]").keyup(function(e){ var key = e.which ? e.which : event.keyCode; if(key == 110 || key == 188){ e.preventDefault(); var value = $(this).val(); ... -
你需要改变46到44,以便逗号而不是句号... $('.number').keypress(function(event) { var $this = $(this); // this next line... if ((event.which != 44 || $this.val().indexOf(',') != -1) && ((event.which < 48 || event.which > 57) && (event.which != ...
-
喜欢这个? "242452354643663463636346366363463636365636363634656346".replace(/(\d{10})/g,"$1,") // 2424523546,4366346363,6346366363,4636363656,3636363465,6346 Like this? "242452354643663463636346366363463636365636363634656346".replace(/(\d{10})/g,"$1,") // 24 ...
-
function check(Sender,e){ var key = e.which ? e.which : e.keyCode; if(key == 44){ Sender.value += '.'; return false; } } 更新 :如果您在输入框中的任何位置键入,这应该有效 function check(Sender,e){ var ...
-
它取决于你的十进制符号设置,虽然在Chrome中我似乎也可以输入一个点作为小数点: 视窗 OSX It is dependent on your Decimal symbol setting, although in Chrome I seem to be able to type a dot as decimal point too: Windows OSX
-
您可以手动指定分隔符( .和) ,并尝试使用两种格式进行解析: string source = "123,456"; // "123.456" (with dot) will be accepted as well double result; if (double.TryParse(source, NumberStyles.Number, //TODO: you may want to change style new N ...
-
那么不要使用数字过滤器? 否则,如果您希望它是数字类型,请创建自定义过滤器: app.filter('customNumber', function() { return function(value) { return parseInt(value, 10) //convert to int } }) 模板: {{value | customNumber}} Then don't use the number filter? Else, if you want it t ...
-
您想在输入类型编号中使用1.2或2.3等数字吗? 然后您可以使用step属性。 阅读下面的代码 Use this javascript and jquery script: function transformTypedChar(charStr) { return charStr == "," ? "." : charStr; // HERE WE SET WHAT WE ARE GOING ...
-
如何允许用户输入逗号(How to allow user to input comma)[2022-06-27]
对于处理您描述的情况的快速而脏的修复,您可以在视图的开头复制request.POST querydict,从输入中删除有问题的逗号,并将调整后的querydict传递给表单构造函数 - def transaction(request): if request.POST.method == 'POST': post = request.POST.copy() if 'amount' in post: post['amount'] = pos[' ...