【问题标题】:Creating an if/else Statement so Page Changes Based on Cursor Value创建 if/else 语句,以便根据光标值更改页面
【发布时间】:2018-05-30 12:37:46
【问题描述】:

我正在为孩子们制作一个园艺游戏。用户单击他们需要的园艺工具(例如铲子),然后单击田地的背景图像,这将带他们进入下一步/页面(例如犁过的田地的背景图像)。将光标更改为工具图像的 javascript 函数工作正常。但是,我注意到任何点击时背景图像都会发生变化,这不是我想要的。只有当光标具有正确的值(即铲子)时,它才应该改变。

为了解决这个问题,我认为最好使用 if/else 语句形式的第二个函数。

<script>
 function myFunction() {

  document.body.style.cursor = "url(img/Shovel.png), pointer";

}

function changePage() {
  var cursor = document.body.style.cursor;

  if (cursor === "url(img/Shovel.png)") {
    window.location.href = "Potato1.html";
  } else {
    window.location.href = "Potato0.html";
  }
}

它没有像我想象的那样工作,页面自动切换到下一个。我想知道我的逻辑是否有意义,所以我会很感激一些指导。

【问题讨论】:

  • 很奇怪它会基于光标......
  • 你到底为什么要这么做?为什么要根据光标改变页面位置??
  • 您应该将选择的项目存储在某种变量中,并将 onlcick 事件添加到图像中。如果您检查 variable == "shovel" 或其他任何内容,并根据您是否继续进行
  • 我建议只改变背景。如果需要,您可以让背景将行为与特定光标相关联。
  • 我是这样看的:园艺工具种类繁多,孩子需要找到合适的。当他们点击铲子时,他们会收到一条验证消息,并且光标会变为该图像。他们点击他们想要播种的背景图像(土壤),这会改变页面。最初,我只是为背景图像设置了一个 onclick 事件,效果很好。然而,我意识到有人可能在没有选择工具的情况下点击背景图像。所以我的理由是我应该将页面的变化与光标绑定以避免这种情况。

标签: javascript if-statement onclick qtextcursor


【解决方案1】:

点击后会显示最新的悬停框的位置:

var current = undefined;

document.body.addEventListener('click', fn, true);

$('#topLeft').hover(function(){
  current = 'topLeft'
})
$('#topRight').hover(function(){
  current = 'topRight'
})
$('#bottomLeft').hover(function(){
  current = 'bottomLeft'
})
$('#bottomRight').hover(function(){
  current = 'bottomRight'
})

function fn(){
  console.log(current)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div style="width:100px; height:100px;">
  <input id="topLeft" style="width:30px; height: 30px; background-color:green;" />
  <input id="topRight" style="width:30px; height: 30px; background-color:red;" />
  <input id="bottomLeft" style="width:30px; height: 30px; background-color:yellow;" />
  <input id="bottomRight" style="width:30px; height: 30px; background-color:blue;" />
</div>

对于您的具体问题,只需在有人点击工具时设置变量:

$('#shovel').click(function(){
    current = 'shovel'
})

然后在点击土壤时检查变量

$('#soil').click(function(){
    if(current === 'shovel')
        [whatever]
    else
        [whatever 2]
})

注意:

您将光标设置为"url(img/Shovel.png), pointer",然后将其与"url(img/Shovel.png)" 进行比较。此时您是否尝试调试cursor 的值?

document.body.style.cursor = "url(\"img/Shovel.png\"), pointer";

console.log(document.body.style.cursor) // url("img/Shovel.png"), pointer

var result = (document.body.style.cursor === "url(\"img/Shovel.png\")")

console.log(result) // false

result = (document.body.style.cursor === "pointer")

console.log(result) // false

result = (document.body.style.cursor === "url(\"img/Shovel.png\"), pointer")

console.log(result) // true

【讨论】:

  • 它也返回 false 。正如您在上面指出的那样,我知道它们是不同的,但我不确定我是否理解这是如何将值呈现为假的。对我来说,第一行意味着要么将光标更改为图像铲子,要么更改为指针(如果不可能)。如果光标已经改变,那么 var 结果应该是 true,不是吗?
  • @user9192911 不,cursor 中的值 将是 "url(img/Shovel.png), pointer",它是 渲染 将是 Shovel.png图片或pointer
  • 感谢您的解释,细微差别非常微妙。我认为它被理解为两个可能的值,而不是一个。我还运行了其他编辑,它们也返回 false。我会看看你上面建议的解决方案,把它放在 JS 中,因为我不使用 jQuery 并尝试一下。谢谢。
  • @user9192911 你确定最后一个返回false?它为我返回true
  • 我又重复了一次练习,它返回了 3 次 false...在代码 sn-p 中也这样做了,它也返回 false。
猜你喜欢
  • 1970-01-01
  • 2012-07-21
  • 2021-06-30
  • 1970-01-01
  • 2020-04-05
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 2016-08-23
相关资源
最近更新 更多