博客
关于我
jQuery库的基础知识
阅读量:118 次
发布时间:2019-02-26

本文共 747 字,大约阅读时间需要 2 分钟。

jQuery库的核心方法——$()

jQuery是前端开发中极为流行的库之一,其核心方法“$()”是所有操作的起点。这个方法可以用来选择页面元素,也可以执行各种功能方法。无论是选择元素还是调用内置方法,代码都以“$”和圆括号“()”开始。实际上,“$()”是jQuery库中“jQuery()”方法的简写,用于创建一个空的jQuery对象。

jQuery代码的风格

在编写jQuery代码时,需要注意以下几点:

  • 连缀方式:大多数功能方法都返回jQuery对象,这样可以链式调用多个方法。例如:
    $('#box').css('color','red').css('font-size','50px');
  • 注释
    • 单行注释://…
    • 多行注释:/* … */

jQuery库延迟等待加载模式

在实际应用中,为了确保代码在页面加载完成后才能执行,通常会将方法包裹在$(function(){})中。这种方式称为“延迟等待加载模式”。

  • 实现方式
    $(function() {  // 放置需要执行的代码});
  • 与window.onload的区别
    • 执行时机$(document).ready()会在页面内容完全加载后执行,而window.onload必须等待所有资源(包括图片)加载完成。
    • 执行次数$(document).ready()可以多次执行,而window.onload只能执行一次。
    • 简写方式$(document).ready()可以简写为$(function(){})

总结

jQuery库的核心方法“$()”是其操作的基础,代码风格注重连缀和注释的规范性。延迟等待加载模式通过$(document).ready()确保代码在页面内容加载完成后执行,避免了在元素未加载前导致的错误。

转载地址:http://mmoy.baihongyu.com/

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>