简体   繁体   English

香草javascript中的each()问题

[英]Each() issue in vanilla javascript

Everytime I try to reproduce .each() jQuery function in vanilla JavaScript, I'm in trouble. 每当我尝试在.each() JavaScript中重现.each() jQuery函数时,我都会遇到麻烦。

When I try to change this : 当我尝试更改此内容时:

$("[data-lng]").each(function(){
   var lng = $(this).data('lng');
   $('#language').text(lng)
});

To this : 对此:

var elem = document.querySelectorAll("[data-lng]");
Array.prototype.forEach.call(elem, function(){
    document.getElementById('language').write = elem.dataset.lng
});

Console returns elem.dataset is not defined 控制台返回elem.dataset is not defined

Plus, I'm dealing with data stuff so I'm not even sure if its legal to write this document.querySelectorAll("[data-lng]") 另外,我正在处理data所以我什至不确定编写此document.querySelectorAll("[data-lng]")是否合法document.querySelectorAll("[data-lng]")

Thanks for your help ! 谢谢你的帮助 !

PS : Here is an example of what I want to convert into vanilla JS : https://jsfiddle.net/x93oLad8/4/ PS:这是我要转换为原始JS的示例: https : //jsfiddle.net/x93oLad8/4/

First of all, instead of using: 首先,不要使用:

Array.prototype.forEach.call(elem, function()

You can just use 你可以用

elem.forEach(function()

Secondly, callback function can accept arguments (3 of them to be specific): el - current elem (which is the "this" you are looking for? :)) index - index of current elem in array list - the nodelist you loop over 其次,回调函数可以接受参数(其中3个参数是特定的):el-当前元素(您正在寻找的“ this”?:))索引-数组列表中当前元素的索引-循环遍历的节点列表

Usage: 用法:

elem.forEach(function(el, index, list){
    console.log(el); //logs current element
});

Read more: https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach 了解更多: https : //developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach

@Edit: Since there was a big discussion in the comments about the first part, i feel obligated to link a thread on overriding default methods in JS: [].slice or Array.prototype.slice (aka why elem.forEach might not work, just in case :)) @Edit:由于在第一部分的评论中进行了大讨论,我感到有义务在JS中覆盖默认方法上的线程链接: [] .slice或Array.prototype.slice (又称elem.forEach可能不起作用, 以防万一 :))

Its fairly trivial to swap your jsFiddle example out for vanilla JS. 将您的jsFiddle示例换成普通JS相当简单。 One 'gotcha' to be aware of is that IE has no support for NodeList.prototype.forEach() hence using a regular for loop instead. 要注意的一个“陷阱”是IE不支持NodeList.prototype.forEach(),因此使用了常规的for循环。 (See: https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach ) (请参阅: https : //developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach

 var dictionary = { 'greet': { 'it': 'Ciao', 'en': 'Hello', 'fr': 'Salut', } }; var langs = ['it', 'en', 'fr']; var current_lang_index = 0; var current_lang = langs[current_lang_index]; window.change_lang = function() { current_lang_index = ++current_lang_index % 3; current_lang = langs[current_lang_index]; translate(); } function translate() { /* jQuery: $("[data-translate]").each(function(){ var key = $(this).data('translate'); $(this).html(dictionary[key][current_lang] || "N/A"); });*/ /* vanilla */ var dt = document.querySelectorAll("[data-translate]"); //iterate over the NodeList: for (i = 0; i < dt.length; ++i) { var key = dt[i].getAttribute('data-translate');//get the key dt[i].innerHTML = (dictionary[key][current_lang] || "N/A");//set the text } } translate(); 
 <div data-translate="greet"></div> <button onclick="change_lang()">Change Language</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM