博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
你不需要jQuery(二)
阅读量:7174 次
发布时间:2019-06-29

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

完全没有否定jQuery的意思,jQuery是一个神奇的、非常有用的工具,可以节省我们大量的时间。

但是,有些时候,我们只需要jQuery的一个小功能,来完成一个小任务,完全没有必要加载整个jQuery程序库。下面是一些用简单JavaScript实现jQuery功能特征的代码汇总。当然,这个汇总并不完全,你最好还是看一下《》

查找搜索(选择器)

按ID查找:

$('#foo')document.getElementById('foo')

按class名搜索:

$('.bar')document.getElementsByClassName('bar')

按标记名搜索:

$('span')document.getElementsByTagName('span')

按子元素搜索:

$('#foo span')document.getElementById('foo').getElementsByTagName('span')

搜索特殊元素:

$('html')document.documentElement$('head')document.head$('body')document.body

元素属性操作

获取/设置HTML:

$('#foo').html()document.getElementById('foo').innerHTML$('#foo').html('Hello, world!')document.getElementById('foo').innerHTML = 'Hello, world!'

添加/删除/搜索判断class:

$('#foo').addClass('bar')document.getElementById('foo').className += ' bar '$('#foo').removeClass('bar')document.getElementById('foo').className = document.getElementById('foo').className.replace(/\bbar\b/gi, '')$('#foo').hasClass('bar')document.getElementById('foo').className.search(/\bbar\b/gi) !== -1

元素值:

$('#foo').val()document.getElementById('foo').value

 

特效

隐藏/显示操作:

$('#foo').show()document.getElementById('foo').style.display = ''$('#foo').hide()document.getElementById('foo').style.display = 'none'

 修改CSS样式:

$('#foo').css('background-color', 'red')document.getElementById('foo').style.backgroundColor = 'red'

  

事件

页面加载完成(ready)

在jQuery里,我们最常使用的是$(document).ready。对于它,下面是替换方法:

document.onreadystatechange = function() {    if (document.readyState === 'complete') {        // DOM is ready!    }};

  

点击事件

$('#foo').click(function() { ... })document.getElementById('foo').onclick = function() { ... }

 

 

AJAX

新版的JavaScript API里提供了一个全新的可以实现ajax的API——,这个api采用了全新的  架构,使用起来更方便,更灵活,详细用法请参考《》。

工具类技术

分析JSON:

jQuery.parseJSON(json)JSON.parse(json)

 

总结

从上面的代码,我们可以看出,jQuery里的很多功能都可以用很多简单的JavaScript代码实现。jQuery虽然很好用,但它有体积的负担,如果遇到一个问题,你可以用简单的代码实现,而不用去加载jQuery,这岂不是更高效,更实用的方法吗

转载于:https://www.cnblogs.com/xupeiyu/p/5067105.html

你可能感兴趣的文章
重置Freebsd及LINUX系统ROOT口令丢失
查看>>
我的家庭私有云计划-20
查看>>
如何在Evolution中加密(二)
查看>>
WindowsServer2003虚拟机 安装SQL2005失败 终于找到原因了
查看>>
讨论:国内使用移动设备做软Token的前途
查看>>
Zabbix使用外部邮箱服务器发送邮件报警
查看>>
NeHe OpenGL第三十六课:渲染到纹理
查看>>
使用ntop代替sniffer与wireshark来监控网络
查看>>
C#学习之接口
查看>>
凡事预则立
查看>>
[IE技巧] IE 除了用来上网之外,还可以用来做计算器
查看>>
Java字符串与文件的互转
查看>>
[零基础学JAVA]Java SE实战开发-37.MIS信息管理系统实战开发[JDBC](2)
查看>>
redis的导入导出需要特别注意的地方
查看>>
实战:RIP和EIGRP路由再发布
查看>>
SpringCloud中文社区转型Spring4All欢迎您的加入
查看>>
ntop安装过程
查看>>
《统一沟通-微软-实战》-6-部署-5-边缘服务器-2012-07-12-2-A
查看>>
WinForm自动化测试工具开发札记(3)
查看>>
CentOS5.5环境下布署LVS+keepalived
查看>>