没有document.ready为什么,jQuery函数无法正常工作? [英] jquery functions are not working without document.ready why.?

查看:60
本文介绍了没有document.ready为什么,jQuery函数无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

///直到我们使用Document.ready

//This code is not working until we are doing using Document.ready

$('.a').on('click',function(){
   $('.a').fadeOut(200);
});

$(document).ready(function () {
   $('.a').on('click', function() {
   $('.shad').fadeIn(1000);
   });
});

推荐答案

它是由于尝试在DOM中不存在的元素上创建事件而引起的.使用$(document).ready来确保DOM中存在元素.

It caused by trying to creating an event on element that doesn't exists in DOM. use $(document).ready to make sure that elements existing in your DOM.

$(document).ready(function () {
   $('.a').on('click', function() {
   $('.a').fadeOut(200);
   $('.shad').fadeIn(1000);
   });
});

这篇关于没有document.ready为什么,jQuery函数无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆