[英]jQuery selector caching issue
問題是,當我每次寫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");
});
});
順便說一下,控制台沒有錯誤。 我也在使用jQ-UI
你需要在dom
准備好時cache
它們。
$(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");
});
});
當你緩存它們時,它們會在document.ready之前被評估。 嘗試這個:
var NewFormContainer="#NewUserFormContainer", opener="#nufcOpen", closer="#nufcClose", NewForm="#NewUserForm";
$(function() {
opener = $(opener);
closer = $(closer);
NewFormContainer = $(NewFormContainer);
NewForm = $(NewForm);
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.