document.getElementById('main').addEventListener('click', dothis);
function dothis(){
// now in jQuery
$(this).children().each(function(){
if($(this).is('.focused') settingsPanel();
});
}
但这似乎效率低下,特别是在 #main 有很多孩子的情况下。
那么这是正确的做法吗?
document.getElementById('main').addEventListener('click', doThis);
function doThis(event){
if($(event.target).is('.focused') || $(event.target).parents().is('.focused') settingsPanel();
}
function shuffle(array) {
let currentIndex = array.length;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
}
// Used like so
let arr = [2, 11, 37, 42];
shuffle(arr);
console.log(arr);
document.querySelector('#main').addEventListener('click', (e) => {
if (!e.target.closest('.focused')) {
return;
}
// code of settingsPanel here, if it isn't too long
});