This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?

(9个答案)


3年前关闭。




我看不出我在做什么错

var newcustomerform = $('#newcustomerform');

//hide new customer form
function hideForm(){
    newcustomerform.hide();
}

$(function(){
hideForm(); //this doesnt work
});


这里有什么问题?

最佳答案

您必须等待DOM加载后才能尝试指向DOM元素。为此,请将var newcustomerform = $('#newcustomerform');放在$(function(){
即在您调用hideForm()的上方

10-04 21:51