jQuery选择器缓存问题

时间:2012-09-04 16:59:30

标签: javascript jquery jquery-ui caching jquery-selectors

问题是,当我每次写HTML标签'id时,下面的代码都有效。但是当我缓存它们时,却没有。我错过了什么?

这是我的代码:

var NewFormContainer=$("#NewUserFormContainer"), opener=$("#nufcOpen"), closer=$("#nufcClose"), NewForm=$("#NewUserForm");
$(function() {
    $( "#userType" ).buttonset();
    $(".btn").button();

    closer.button({
        icons: {
            primary: "ui-icon-closethick"
        }, text: false
    }).click(function(){
        NewFormContainer.slideUp("slow");
    });
    opener.click(function(){
        NewFormContainer.slideDown("slow");
    });
});

BTW,控制台没有错误。我也在使用jQ-UI

2 个答案:

答案 0 :(得分:2)

cache准备好后,您需要dom

$(function() {
    $( "#userType" ).buttonset();
    $(".btn").button();

    var NewFormContainer=$("#NewUserFormContainer"),
        opener=$("#nufcOpen"),
        closer=$("#nufcClose"),
        NewForm=$("#NewUserForm");

    closer.button({
      icons: {
        primary: "ui-icon-closethick"
      }, text: false
    }).click(function(){
        NewFormContainer.slideUp("slow");
    });

    opener.click(function(){
        NewFormContainer.slideDown("slow");
    });
});

答案 1 :(得分:1)

当你缓存它们时,它们会在document.ready之前被评估。试试这个:

var NewFormContainer="#NewUserFormContainer", opener="#nufcOpen", closer="#nufcClose", NewForm="#NewUserForm";
$(function() {
  opener = $(opener);
  closer = $(closer);
  NewFormContainer = $(NewFormContainer);
  NewForm = $(NewForm);
  ...