开发者

Run Javascript inside jQuery

I have the following function in Javascript:

$find('mainWindow').repaint();

I need to run that inside this jQuery function:

    $("#tools").click(function() {
     开发者_C百科   if(get_cookie('visible')== null) {
            set_cookie('visible','no',2020,1,1,'/','.domain');
            $("#WinMain").animate({top: "25px"}, 200);
            **<!--INSERT FIND-->**
        } else {
            delete_cookie('visible','/','.domain');
            $("#WinMain").animate({top: "89px"}, 200);
            **<!--INSERT FIND-->**
        }


A click handler is just that, an event handler...no one said you can have only one :) If multiple are attached, they'll run in the order they were bound...so if you can't modify that function, just attach your own .click() handler after the current one, like this:

$("#tools").click(function() {
  if(get_cookie('visible')== null) {
    set_cookie('visible','no',2020,1,1,'/','.domain');
    $("#WinMain").animate({top: "25px"}, 200);
  } else {
    delete_cookie('visible','/','.domain');
    $("#WinMain").animate({top: "89px"}, 200);
  }
});
//add your own handler later:
$("#tools").click(function() {
  $('#mainWindow').repaint();
});

I'm not really sure what $find('mainWindow') stood for, so I'm doing a bit of guessing in the handler above, grabbing it by ID. If you can modify the original handler, just stick the code you want to run in where you have placeholder comments now now.


jQuery is JavaScript, so you can just insert your JavaScript call:

$("#tools").click(function() { 
    if(get_cookie('visible')== null) { 
        set_cookie('visible','no',2020,1,1,'/','.domain'); 
        $("#WinMain").animate({top: "25px"}, 200); 
    } else { 
        delete_cookie('visible','/','.domain'); 
        $("#WinMain").animate({top: "89px"}, 200); 
    }
    $find('mainWindow').repaint(); 
});

Note that I have put it after the if statement, as you wanted it put in both the true and false branch of the if statement.

Is there more to your question?


Why not just

    $("#tools").click(function() {
        if(get_cookie('visible')== null) {
            set_cookie('visible','no',2020,1,1,'/','.domain');
            $("#WinMain").animate({top: "25px"}, 200);
        } else {
            delete_cookie('visible','/','.domain');
            $("#WinMain").animate({top: "89px"}, 200);
        }
        $find('mainWindow').repaint();

? Does this not work? What problems are you having?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜