简体   繁体   English

如何在第一次单击时执行操作并在第二次单击时执行另一个操作

[英]How to make an action on first click and another action on second click

My goal is to move a div element to the right side of the page on the first click and move it back to the left if I click it again and so on.我的目标是在第一次单击时将 div 元素移动到页面的右侧,如果再次单击它,则将其移回左侧,依此类推。 How can I do this in javascript?我怎样才能在 javascript 中做到这一点?

As enhzfelp said in their comment, the best solution would be to create a css class which moves your element to the right side of the page and to add / remove it with javascript.正如 enhzfelp 在他们的评论中所说,最好的解决方案是创建一个 css 类,将您的元素移动到页面的右侧,并使用 javascript 添加/删除它。

If your goal is actually to perform one action and on next event call perform another, you can simply change a variable whenever the event is called.如果您的目标实际上是执行一个操作,而在下一个事件调用时执行另一个操作,则可以在调用事件时简单地更改变量。

Example code:示例代码:

let right = false;

someElement.on('click', () => {
  right = !right;

  if (right) moveRight();
  else moveLeft();
});

function moveRight() { ... }
function moveLeft() { ... }

You can also do this你也可以这样做

<script>
var clickCount = 0;
function checkClick() {
  if ( clickCount % 2 == 0 ) {
       alert("first click");
  } else {
      alert("Second click");
  }

  clickCount++
}
</script>

<button onclick="checkClick()">Click me</button>

Hmm, my english is kinda weak so I'll try my best to explain.嗯,我的英语有点弱,所以我会尽力解释。

Let's assume we have a div with id moveable假设我们有一个 id 为moveablediv

<div id="moveable"></div>

And we have button我们有按钮

<button id="move">Move</div>

And our goal is to move the div to the right in the first click, and in the second click we will move it back to the right.我们的目标是在第一次单击时将div向右移动,在第二次单击时将其移回右侧。

var div = document.getElementById("moveable");
var button = document.getElementById("move"):

button.addEventListener("click", function() {
if (div.getAttribute("data-moved") && div.getAttribute("data-moved") == true){
div.setAttribute("data-moved", "false");
div.style.float = "left";
} else {
div.setAtteibute("data-moved", "true")
div.style.float = "right"
}
})

So, as I understood, you want to make so on click, the div element move to the opositive direction of left or right.所以,据我所知,你想在点击时使 div 元素向左或向右的正方向移动。

You can make this with an event listener, that listens to the click event and execute whatever you want on every click.您可以使用事件侦听器进行此操作,该侦听器侦听单击事件并在每次单击时执行您想要的任何操作。

document.addEventListener('click', function(event) {
  // your code that executes on every click
});

This event listener listens the click on all your webpage, so if you want to only listen the click when the use click your div, you need to get the div.这个事件监听器监听你所有网页上的点击,所以如果你只想在用户点击你的 div 时监听点击,你需要获取 div。 There are several ways, but I recomend you to add an id to the div.有几种方法,但我建议您在 div 中添加一个 id。

<div id="iAmYourElement"></div>

And then get the element in JavaScript然后在 JavaScript 中获取元素

const element = document.getElementById("iAmYourElement");
element.addEventListener('click', function(event) {
  // your code that executes on every click
});

Now that we have the way to deal with the click event, let's talk about the code that goes inside the listener.现在我们有了处理点击事件的方法,让我们来谈谈进入侦听器内部的代码。

One way to do this is creating two CSS classes, one is your div on the left and another whe div on your right.一种方法是创建两个 CSS 类,一个是左侧的 div,另一个是右侧的 div。 So, if we need the element to be on the right, we add the right-class, and if we need the div to be to the left, we add left-class and remove right-class.所以,如果我们需要元素在右边,我们添加 right-class,如果我们需要 div 在左边,我们添加 left-class 并删除 right-class。

Final javascript code will looks like that:最终的 javascript 代码将如下所示:

const element = document.getElementById("iAmYourElement");

let isDivOnLeft = true;
element.addEventListener('click', function (event) { // executes if you click on the div
  isDivOnLeft = !isDivOnLeft // We negate the value of isDivOnLeft, so if it was true, it will now be false and vice versa.
  if (isDivOnLeft) {
    elementToRight()
  } else {
    elementToLeft()
  }
});

function elementToRight() {
  element.classList.remove("left-class")
  element.classList.add("right-class")
}

function elementToLeft() {
  element.classList.remove("right-class")
  element.classList.add("left-class")
}

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

相关问题 单击第一个操作,单击第二个按钮 - Button first action on click and second action on click 第二次单击时触发Ng单击触发器操作(不是第一次) - Ng-click trigger action on second click (not on first) 执行单击(检查出现)动作,再次执行第二次单击(检查消失)动作。 需要,第二次点击删除第一个动作 - On-click (check appears) action is performed, second click (check disappears) action is performed again. Need, second click to delete first action 如何阻止/关闭点击动作1秒钟 - How to block/shut down click action for 1 second Jquery - 防止<a>第一次单击时的默认操作,允许第二次单击时执行默认操作</a> - Jquery - prevent default action of <a> on first click, allow default action on second click 第二次单击后的操作按钮 - action button after the second click 修改 jQuery 中的第二次点击动作 - Modify second click action in jQuery 如何在点击时切换动作 - How to toggle action on click 如何在第一次单击中运行jquery函数,然后在第二次单击中运行另一个函数 - How to run a jquery function in first click and another function in second click 仅在触摸事件上防止第一次单击时的链接操作并允许第二次单击 - Prevent a link action on first click only on touch events and allowing second click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM