-1

So basically what I need to do, is when I click on one UL list item, while holding Ctrl it needs to duplicate and that copy needs to go to another list, so far I've got to where only when I click on something it goes to another list with no Ctrl which is also mandatory for this homework. I'll upload script what I have so far:

var usersA = document.getElementById("users-a");
var usersB = document.getElementById("users-b");

var onclickA;
var onclickB = function() {
        usersA.appendChild(this);
        this.onclick = onclickA;
        return false;
    };
onclickA = function() {
        usersB.appendChild(this);
        this.onclick = onclickB;
        return false;
    };

for (var i=0; i < usersA.getElementsByTagName("li").length; i++)
    usersA.getElementsByTagName("li")[i].onclick = onclickA;
for (var i=0; i < usersB.getElementsByTagName("li").length; i++)      
    usersB.getElementsByTagName("li")[i].onclick = onclickB;

Any suggestions on how to do it with the least amount of code as possible? I mean do I need to create another event for that, I am lost, Thanks for any help!

4

1 回答 1

0

就像你说的我理解的。如果ctrl按下键并li单击元素。基本上没有这样的方法来检测ctrl键是否被按住。但是下面的这个技巧将在 keydown 上返回 true,在 keyup 上返回 false。因此,当ctrl键被按住时,它总是正确的,直到它被释放

var keys = {};
onkeydown = onkeyup = function(e){
    e = e || event;
    keys[e.keyCode] = e.type == 'keydown';
    if(keys[17] == true){
        //the ctrl key is held down, so run your code here
    }
}
于 2016-11-23T11:05:40.223 回答