jquery click on href link - have to click twice

故事扮演 提交于 2019-12-22 18:08:39

问题


The function works, but the popup will open only after the button is clicked twice (and then, subsequent clicks get the action on the first click).

$(document).ready(function(){
    $('a#printPosBtn').on('click', function(e) {
        e.preventDefault();
        $('.printPopup').popupWindow({ 
            centerBrowser:1,
            height:500,
            width:720,
            scrollbars: 1,
            resizable: 1
        });
        return false;
    });
});

What's wrong?


回答1:


I think that is because you are actually initialising the plugin within the click handler. From a quick skim through the popupWindow docs it appears that the plugin takes care of binding a click handler for you, which means that your first click binds the popup functionality (including an onclick handler) so it only works upon click a second time. I would try:

$(document).ready(function() {

    $(".printPopup").popupWindow({
        centerBrowser: 1,
        height: 500,
        width: 720,
        scrollbars: 1,
        resizable: 1
    });

    // open popup by clicking on some other element
    $('#printPosBtn').on('click', function(e) {
        e.preventDefault();
        $(".printPopup").click();         
    });

});​


来源:https://stackoverflow.com/questions/9251916/jquery-click-on-href-link-have-to-click-twice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!