google chrome extension - [Javascript]Passing local vars as parameters to EnventListener Click -
I have to give a local variable value in the function.
My code can only alert local versions when it is not used as a parameter for any other function.
When I try to pass the value.
Here is my code, this file is from 'test.js':
function testing () {var divs = document.getElementsByClassName ('post'); {If (divs [i] .id! = Null) for {var i = 0; i & lt; divs.length; i ++} {var element = document.createElement ("div"); Var currentId = divs [i] .id; Element.className = 'myclass'; // warning (currentId); & Lt; - True value element.addEventListener ("click", function () {warning (currentId); // <- value == null ?!}, incorrect); Divs [i] .appendChild (element); }}}}
This code is from the manifest:
"content_scripts": [{... "js": ["Test.js"]} If I use divs [i], Chrome console reports directly instead of currentId var:
Unkit Type error: property can not be read 'id' of 'unknown' (anonymous function)
"one Close the loop "problem (see your original post on my comment)
Try it
function testing () {var divs = document.getElementsByClassName ('post' ); {If (divs [i] .id! = Null) for {var i = 0; i & lt; divs.length; i ++} {var element = document.createElement ("div"); Var currentId = divs [i] .id; Element.className = 'myclass'; // warning (currentId); & Lt; - true value element.addEventListener ("click", functionFactory (currentId), incorrect); Divs [i] .appendChild (element); }}} Function functionfile (_localId) {return function () {warning (_localId); }}
Comments
Post a Comment