曾经的超级明星类库jQuery未来也许不再会被前端程序猿追捧了!
添加页面元素
jQuery代码:
$('i').prepend('<div>--New Element--</div>');
JS代码:
var parent = document.querySelector(".container"); var p = document.createElement("p"); parent.prepend("Some text", p);
注意这个方法目前还是实验阶段,可能你的浏览器并不支持需要使用polyfill来使得浏览器支持
删除页面元素
jQuery代码:
$('i').remove();
JS代码:
elem.remove();
插入页面元素
jQuery代码:
$elem.before($someOtherElem);
JS代码:
elem.before(someOtherElem);
替换页面元素
jQuery代码:
$elem.replaceWith($someOtherElem);
JS代码:
elem.replaceWith(someOtherElem);
找到最近的匹配元素
jQuery代码:
$elem.closest("div");
JS代码:
elem.closest("div");
目前的浏览器支持
如果想看看浏览器对以上API的支持程度,大家可以使用caniuse来查看jquery风格的DOM操作的支持兼容性情况
淡入淡出效果
jQuery代码:
$elem.fadeIn();
CSS代码:
.thingy {
display: none;
opacity: 0;
transition: .8s;
}
JS代码:
elem.style.display = "block"; requestAnimationFrame(() => elem.style.opacity = 1);
只绑定一次事件
jQuery代码
$elem.one("click", someFunc);
JS代码(过去使用的方式)
function dostuff() { alert("some stuff happened"); this.removeEventListener("click", dostuff); } var button = document.querySelector("button"); button.addEventListener("click", dostuff);
JS代码(现代使用的简化版本)
elem.addEventListener('click', someFunc, { once: true, });
或者
elem.addEventListener('click', myClickHandler, {
once: true,
capture: true
});
动画效果
jQuery
$elem.animate({
width: "20%",
opacity: 0.1,
marginLeft: "0.6in",
fontSize: "3em",
borderWidth: "10px"
}, 500);
JS
var elem = document.querySelector('.animate-me'); elem.animate([ { transform: 'translateY(-1000px) scaleY(2.5) scaleX(.2)', transformOrigin: '50% 0', filter: 'blur(40px)', opacity: 0 }, { transform: 'translateY(0) scaleY(1) scaleX(1)', transformOrigin: '50% 50%', filter: 'blur(0)', opacity: 1 } ], 1000);
Ajax请求处理
jQuery代码
$.ajax('https://some.url', {
success: (data) => { /* do stuff with the data */ }
});
JS代码
fetch('https://some.url')
.then(response => response.json())
.then(data => {
// do stuff with the data
});
当然上面有部分JS代码在浏览器中执行的可能并不完整,但是基本所有的javascript都可以找到对应的polyfill来解决相关的兼容性问题,如下:
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
欢迎访问GBin1.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2013-08-23 17种新型的响应式开发框架
2013-08-23 使用jQuery创建模态窗口登陆效果