开发者

I keep loosing my events, how can I keep them attached

I have a small 'screen' that pops up over part of my page. This screen has two buttons on it. When either of these buttons get pressed the screen is removed. But the screen could need to be reused at a later time so instead of always rebuilding the screen i store it in data of an object on the page. While the stored screen reloads fine, all of the even开发者_如何转开发ts seem to be missing as the second time the screen appears the buttons dont do n e thing. How can i fix this?

function AddScreen() {
    var store= $('#store'), screen;
    if(objectStoringScreen.length > 0){
        screen = objectStoringScreen.data('screen');
    }else {
        store = CreateStore(); //Create Store
        $(this).parent().append(store); //Add Store
        screen = Screen(); // Create Screen
    }

    PushScreen(screen); //Load selected screen

    function Screen() {
        //build div
        // build two buttons and place them in the div
        // btn1.click(onOK); btn2.click(onCancel);
    }

    function onOK() {
        store.data('screen',screen); 
        PopScreen();
    }
    function onCancel() {
        PopScreen(); 
    }
}


I would just create the screen beforehand and toggle it when needed.

<div id="screen">
    Screen Div
    <input type="button" value="OK" /> <input type="button" value="Cancel" />
</div>
<div id="showScreen">click here to show the screen</div>

    $(document).ready(function () {
        // Screen already exists, just hide it.  If you need to build it from data on the page, do it after it's hidden.
        $('#screen').hide();

        // Some action that shows/hides your screen, I just used a click method as an example
        $('#showScreen').click(function () {
            if ($('#screen').css('display') == 'none') {
                // Or you can build here if you need it to be updated in real time for whatever reason
                //   ex.  $('#screen').append('<input type="button" id="someSpecialButton" value="Special Button" />');
                $('#screen').show();
            }
            else {
                $('#screen').hide();
            }
        });

        // Screen button events, will still exist after hiding the screen
        $('#screen input[type="button"]').click(function () {
            alert('You clicked ' + $(this).attr('value') + ' on the screen');
        });
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜