Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击事件撤消_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 单击事件撤消

Javascript 单击事件撤消,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我在我的第一个网站上工作,点击事件有问题 这是我的网站(尚未完成): 问题是取消不起作用(函数在单击后运行,我用警报检查了它,但单击事件根本不会撤消它本身。我也尝试使用.on.off方法,但结果相同。如果它出错,或者我无法使用这些方法正确撤消此“单击”函数?如果没有,我应该如何执行?提前感谢您的答复,我将自己尝试解决它,等待你的建议 var chooseHobby = function() { $(this).addClass('hobbyOnClick').removeClass('hob

我在我的第一个网站上工作,点击事件有问题

这是我的网站(尚未完成):

问题是取消不起作用(函数在单击后运行,我用警报检查了它,但单击事件根本不会撤消它本身。我也尝试使用.on.off方法,但结果相同。如果它出错,或者我无法使用这些方法正确撤消此“单击”函数?如果没有,我应该如何执行?提前感谢您的答复,我将自己尝试解决它,等待你的建议

var chooseHobby = function() {
  $(this).addClass('hobbyOnClick').removeClass('hobby firstInLine hobbyImg');
  $('.hobbyImg').hide();
  $('.hobby').css('display', 'none');
  updateHeight2();
  var id = this.id;

  if (id == 'sport') {
    if (document.getElementById("gym")) {
      return;
    } else {
      $('#sport').append('<img id="runmaggedon" src="img/runmaggedon.jpg" alt="runmaggedon" />');
      $('#sport').append('<img id="gym" src="img/gym.jpg" alt="gym" />');
      $('#sport').append('<div class="sportText">\n\
            <p>Runmaggedon is my hobby for over a year, it is challenging, hard and the people and athmosphere there is just great. For now my best distance is 24 km in mountain terrain, but it was not my last word! </p>\n\
            \n\
            </div>');
      $('#sport').append('<div class="sportText"><p>Working out is something that I&#39m doing since studies. It is became the part of my daily routine, I love to work with my body and see physical ad power progress. Gym also help with self-discipline and well-being </p></div>');
      $('#sport').append('<div id="cancel"><p>CANCEL</p></div>');
    }
  } else if (id == 'travel') {
    alert("travel");
  } else if (id == 'objectivism') {
    alert("objectivism");
  } else if (id == 'engineering') {
    alert("engineering");
  } else if (id == 'programming') {
    alert("programming");
  } else if (id == 'economy') {
    alert("economy");
  }

  $("#cancel").bind("click", function() {
    alert("function start");
    $(".hobby").unbind("click", chooseHobby);
  });
};

$(document).ready(function() {
  $(".hobby").bind('click', chooseHobby);
});
var chooseHobby=function(){
$(this).addClass('hobbyOnClick').removeClass('hobby firstInLine hobbyImg');
$('.hobbyImg').hide();
$('.hobby').css('display','none');
updateHeight2();
var id=this.id;
如果(id=‘运动’){
if(document.getElementById(“gym”)){
返回;
}否则{
$(“#运动”)。附加(“”);
$(“#运动”)。附加(“”);
$('#sport')。附加('\n\
Runmaggedon是我一年多以来的爱好,它充满挑战,艰苦,那里的人和气氛非常棒。目前,我在山区的最佳距离是24公里,但这不是我的最后一句话!

\n\ \n\ '); $(“#运动”).append(“锻炼是我自学习以来一直在做的事情。它已成为我日常生活的一部分,我喜欢锻炼身体,看到身体和力量的进步。健身房也有助于自律和健康

”); $(“#运动”)。追加(“取消”

”); } }否则,如果(id=‘旅行’){ 警惕(“旅行”); }else if(id==“客观主义”){ 警惕(“客观主义”); }否则如果(id=‘工程’){ 警报(“工程”); }else if(id==‘编程’){ 警报(“编程”); }否则,如果(id=‘经济’){ 警惕(“经济”); } $(“#取消”).bind(“单击”,函数(){ 警报(“功能启动”); $(“.hobby”)。取消绑定(“单击”,选择hobby); }); }; $(文档).ready(函数(){ $(“.hobby”).bind('单击',选择hobby); });
A
单击
是一个事件。它发生在用户单击后的毫秒(相当瞬间)

你不能“撤销”它

对于该事件,您可以注册一个要执行的函数。现在要“撤消”该函数所做的更改,您必须存储以前的相关状态/值。使用另一个
单击
事件,您可以给人一种撤消的印象

下面是一个非常简单的示例,它只更改
文本

//存储初始文本。
var previousH1state=$(“h1”).text();
//修改
$(“#修改”)。在(“单击”,函数(){
$(“h1”).text(“我被修改了!”);
});
//撤消
$(“#撤消”)。在(“单击”,函数()上){
$(“h1”).text(previousH1state);//注意这里使用的是存储的文本。
});
//从“修改/撤消”按钮取消注册与单击事件关联的函数。
$(“#关闭”)。打开(“单击”,函数(){
$(“#修改,#撤消”).off(“单击”);
log(“修改和撤消按钮不再工作”);
});

我没变。
修改上面的标题
撤消修改


在较新版本的jQuery中,使用.off()
这两个
.bind()和
.unbind()
已被弃用。使用
.on()
.off()
代替这是一个很大的
问题,否则,正如我写的那样,我尝试使用.on()和.off()方法,结果相同;(顺便说一句,如果你有比“esle-if”更好的解决方案来处理这个问题,那就太好了,我知道它可能很粗糙,但我对更复杂的方法没有经验。