Je ne parviens pas à des fermetures de JS:fermetures de JavaScript et la portée des variables
// arg: an array of strings. each string is a mentioned user.
// fills in the list of mentioned users. Click on a mentioned user's name causes the page to load that user's info.
function fillInMentioned(mentions) {
var mentionList = document.getElementById("mention-list");
mentionList.innerHTML = "";
for (var i = 0; i < mentions.length; i++) {
var newAnchor = document.createElement("a");
// cause the page to load info for this screen name
newAnchor.onclick = function() { loadUsernameInfo(mentions[i]) };
// give this anchor the necessary content
newAnchor.innerHTML = mentions[i];
var newListItem = document.createElement("li");
newListItem.appendChild(newAnchor);
mentionList.appendChild(newListItem);
}
document.getElementById("mentions").setAttribute("class", ""); // unhide. hacky hack hack.
}
Malheureusement, en cliquant sur l'un de ces balises d'ancrage des résultats dans un appel comme celui-ci:
loadUserNameInfo(undefined);
Pourquoi est-ce? Mon objectif est une ancre comme celle-ci:
<a onclick="loadUserNameInfo(someguy)">someguy</a>
Comment puis-je produire ceci?
Mise à jour Cela fonctionne:
newAnchor.onclick = function() { loadUsernameInfo(this.innerHTML) };
newAnchor.innerHTML = mentions[i];
Ceci est l'une des questions les plus courantes et répétitives ici concernant les fermetures, duplicates possible: http://stackoverflow.com/questions/1734749/ http://stackoverflow.com/questions/643542/ http : //stackoverflow.com/questions/1582634/ http://stackoverflow.com/questions/1331769/ http://stackoverflow.com/questions/1552941/ http://stackoverflow.com/questions/750486/ http://stackoverflow.com/questions/933343/ http: //stackoverflow.c om/questions/1579978/ http://stackoverflow.com/questions/1413916/ – CMS