V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
fyl00

菜鸟求助:JS 中想在 click 事件被触发的时候退出函数

  •  
  •   fyl00 · Mar 29, 2014 · 4082 views
    This topic created in 4418 days ago, the information mentioned may be changed or developed.
    自己通过判断一个变量的值来退出,但是没成功

    代码如下

    $(document).scroll(function () {
    viewBottom = $(document).scrollTop() + $(window).height();
    if ((divTop < $(document).scrollTop()) && ( divBottom > viewBottom )){
    target.css({"background":"gray"});
    pullComment.css({"display":"block"});
    var pullCommentClick = null;
    pullComment.click(function(){
    pullComment.css({"display":"none"});
    $(window).scrollTop(summaryBottom);
    pullCommentClick = 1;
    })
    if (pullCommentClick){
    return;
    }
    }
    else {
    target.css({"background":"white"});
    pullComment.css({"display":"none"});
    }
    })
    4 replies    1970-01-01 08:00:00 +08:00
    jsonline
        1
    jsonline  
       Mar 29, 2014 via Android
    把demo放到 jsfiddler 或者 jsbin
    emric
        2
    emric  
       Mar 29, 2014
    把声明放到匿名函数外面.
    ttph1oc
        3
    ttph1oc  
       Mar 29, 2014
    var pullCommentClick=null放到整个作用域外头
    wizaliu
        4
    wizaliu  
       Mar 29, 2014
    作用域不对,click里面改变的是全局变量pullCommentClick,而你在if里面判断的是scroll回调函数里面申明的pullCommentClick,他们不是一个,当然判断不到了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3054 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 00:05 · PVG 08:05 · LAX 17:05 · JFK 20:05
    ♥ Do have faith in what you're doing.