繁体   English   中英

为什么js函数可以在另一个函数中工作而不是单独工作

[英]Why js function works inside of another function but not alone

我下面有这两个函数,无论出于何种原因,windowedBox函数仅在我从另一个函数调用时才起作用。 如果我直接调用windowedBox(),它将不会执行任何操作。 如果我调用getShareButtons(),它将调用windowedBox()并正常工作。 如果有人知道为什么会这样,请帮助我。 :(

注意:如果您想知道为什么它说$ j而不是$它,因为我是这样设置的。

function getShareButtons(){
    var postPath = window.location.pathname;
    var videoTitle = 'asdf';
    var videoURL = 'asdf';

    //Output social button attributes
    $j(".facebook-share").attr('href', 'asdf');
    windowedBox(); <---THIS CALL WORKS
}

function windowedBox() { 
    $j(".facebook-share, .twitter-share").click(function(){
        window.open(this.href, videoTitle, "width=626, height=436", "status=0", "toolbar=0");
        return false;
    });
}

我想这是因为您正在使用变量

videoTitle 

这不是在此函数中声明的,而是在另一个函数中声明的:)

因为在第一个函数中设置了链接href属性,所以会出现错误,但是如果仅调用第二个this.hrefthis.href保持未定义或为空

这样尝试

$j(".facebook-share").attr('href', 'asdf');
var videoTitle = 'Title';

windowedBox();

function windowedBox() { 
    $j(".facebook-share, .twitter-share").click(function(){
        window.open(this.href, videoTitle, "width=626, height=436", "status=0", "toolbar=0");
        return false;
    });
}

暂无
暂无

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

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