简体   繁体   English

使用CSS选择器作为javascript的输入

[英]Using css selectors as inputs for javascript

So far I'm using different buttons for different transitions (each one with individual function) on one DOM element : 到目前为止,我在一个DOM元素上使用了不同的按钮进行不同的转换(每个按钮都有各自的功能):

onButtonClickUp = function(){
    document.querySelector('.cube-rot').style.transform="rotateY(90deg)"
    };
onButtonClickDown = function(){
    document.querySelector('.cube-rot').style.transform="rotateY(-90deg)"
};
onButtonClickRight = function(){
    document.querySelector('.cube-rot').style.transform="rotateX(90deg)"
};
onButtonClickLeft = function(){
    document.querySelector('.cube-rot').style.transform="rotateX(-90deg)"
};
document.querySelector('.show-up').addEventListener( 'click', onButtonClickUp, false);
document.querySelector('.show-down').addEventListener( 'click', onButtonClickDown, false);
document.querySelector('.show-left').addEventListener( 'click', onButtonClickLeft, false);
document.querySelector('.show-right').addEventListener( 'click', onButtonClickRight, false);

I'm firing everything off on DomContentLoaded and it's working, but I'd like to combine the functions into one with different cases; 我将DomContentLoaded上的所有内容解雇了,并且可以正常工作,但是我想将这些功能组合为不同情况的一个。


this is what I've tried 这就是我尝试过的

 function onButtonClick(rot){
    //rot = rUp || rDown|| rLeft ||rRight ;
    if(rot == 'rUp'){rot = "rotateY(90deg)";}
    else if (rot == 'rDown'){ rot = "rotateY(90deg)"}
    else if (rot == 'rLeft'){ rot = "rotateX(90deg)"}
    else if(rot == 'rRight' ){ rot = "rotateX(-90deg)"};

    cubeRot.style.transform=rot
    };


 document.querySelector('.show-up').addEventListener( 'click', onButtonClick('rUp'), false);
 document.querySelector('.show-down').addEventListener( 'click', onButtonClick('rDown'), false);
 document.querySelector('.show-right').addEventListener( 'click', onButtonClick('rRight'), false);
 document.querySelector('.show-left').addEventListener( 'click', onButtonClick('rLeft'), false);

it doesn't work, adding different EventListners resolves in the last overriding previous.. what's the most convenient way to do this? 它不起作用,添加不同的EventListners可以解决上一个覆盖的上一个问题。最方便的方法是什么?

Since you are calling onButtonClick() when you do your event binding, it needs to return a function. 由于在进行事件绑定时正在调用onButtonClick() ,因此它需要返回一个函数。

function onButtonClick(rot){
    return function(e) {
        //rot = rUp || rDown|| rLeft ||rRight ;
        if(rot == 'rUp'){rot = "rotateY(90deg)";}
        else if (rot == 'rDown'){ rot = "rotateY(-90deg)"}
        else if (rot == 'rLeft'){ rot = "rotateX(90deg)"}
        else if(rot == 'rRight' ){ rot = "rotateX(-90deg)"};

        cubeRot.style.transform=rot;
    };
}

Alternatively, leave the function as is and use .bind() : 或者,将函数保留.bind()并使用.bind()

document.querySelector('.show-up').addEventListener('click', onButtonClick.bind(null, 'rUp'), false);
document.querySelector('.show-down').addEventListener('click', onButtonClick.bind(null, 'rDown'), false);
document.querySelector('.show-right').addEventListener('click', onButtonClick.bind(null, 'rRight'), false);
document.querySelector('.show-left').addEventListener('click', onButtonClick.bind(null, 'rLeft'), false);

The above solutions should get you unblocked. 以上解决方案应该使您畅通无阻。 But, there are some more code improvements you could consider. 但是,您可以考虑进行更多代码改进。

Instead of that long if .. else if block, you could store the value in an object and look up the value by a key: 不用那么长的if .. else if块,您可以将值存储在对象中并通过键查找值:

var rotations = {
    rUp: "rotateY(90deg)",
    rDown: "rotateY(-90deg)",
    rLeft: "rotateX(90deg)",
    rRight: "rotateX(-90deg)"
};
function onButtonClick(rot) {
    cubeRot.style.transform = rotations[rot];
}

Your event handler has access to the clicked element via this . 您的事件处理程序可以通过this访问clicked元素。 You don't need to pass a rot argument, you can just look at this.className : 您不需要传递rot参数,只需查看this.className

var rotations = {
    up: "rotateY(90deg)",
    down: "rotateY(-90deg)",
    left: "rotateX(90deg)",
    right: "rotateX(-90deg)"
};
function onButtonClick(e) {
    var rot = this.className.slice(5);
    cubeRot.style.transform = rotations[rot];
}
[].slice.call(document.querySelectorAll("[class^='show-']")).forEach(function(el) {
    el.addEventListener("click", onButtonClick, false);
});

Assuming your buttons are plain HTML buttons with no innerHTML you can do this 假设您的按钮是纯HTML按钮,没有innerHTML,则可以执行此操作

<input type="button" action-type="rUp" value="Show up" class="show-up" />

This adds an attribute to the input with the desired action variable. 这会将具有所需动作变量的属性添加到输入。

function onButtonClick(e){
    var rot = e.target.getAttribute("action-type");
    if(rot == 'rUp'){rot = "rotateY(90deg)";}
    else if (rot == 'rDown'){ rot = "rotateY(90deg)"}
    else if (rot == 'rLeft'){ rot = "rotateX(90deg)"}
    else if(rot == 'rRight' ){ rot = "rotateX(-90deg)"};

    cubeRot.style.transform=rot;
    };


 document.querySelector('.show-up').addEventListener( 'click', onButtonClick, false);
 document.querySelector('.show-down').addEventListener( 'click', onButtonClick, false);
 document.querySelector('.show-right').addEventListener( 'click', onButtonClick, false);
 document.querySelector('.show-left').addEventListener( 'click', onButtonClick, false);

When the event is fired e.target refers to the element that fired the event (the button). 触发事件时, e.target指触发事件的元素(按钮)。 rot stores the attribute action-type . rot存储属性action-type Based upon rot the correct action will be applied. 基于rot将采取正确的措施。 The advantage is that this doesn't require new functions to be created or functions to be wrapped in a bind. 好处是不需要创建新函数或将函数包装在绑定中。

You can use the this object to reference the button that's triggering the event: 您可以使用此对象来引用触发事件的按钮:

  var onButtonClick = function () {
    var button = this;
    document.querySelector('.dir').innerHTML = button.name;
    switch(button.name) {
       case 'up': alert('up'); break;
       case 'down': alert('down'); break;
       case 'left': alert('left'); break;
       case 'right': alert('right'); break;
     }
  };

  document.querySelector('.show-up').addEventListener('click', onButtonClick, false);
  document.querySelector('.show-down').addEventListener('click', onButtonClick, false);
  document.querySelector('.show-left').addEventListener('click', onButtonClick, false);
  document.querySelector('.show-right').addEventListener('click', onButtonClick, false);

Check this plunk: http://plnkr.co/edit/MJzMffnDZJripzXkiZzc 检查这个小东西: http : //plnkr.co/edit/MJzMffnDZJripzXkiZzc

Happy coding! 编码愉快!

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

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