jQuery->JavaScript一览表
比较jQuery与JavaScript的不同功能实现
CSS选择器
元素选择
// jQuery
$("li").css("color", "red");
// JavaScript
document.querySelectorAll('li').forEach(el => {
el.style.color = 'red';
});
ID选择
// jQuery
$('li#first').css('color', 'red');
// JavaScript
document.querySelector('li#first').style.color = 'red';
类选择
// jQuery
$('li.first').css('color', 'red');
// JavaScript
document.querySelectorAll('li.first').foreach(el => {
el.style.color = 'red';
});
子元素选择
// jQuery
$('.first strong').css('color', 'red');
// JavaScript
document.querySelectorAll('.first strong').forEach(el => {
el.style.color = 'red';
});
通配符选择
$('li *').css('color', 'red');
// JavaScript
document.querySelectorAll('li *').forEach(el => {
el.style.color = 'red';
});
分组选择
// jQuery
$('#first, #third').css('color', 'red');
// JavaScript
document.querySelectorAll('#first, #third').forEach(el => {
el.style.color = 'red';
});
伪类选择
// jQuery
$('li:first-child').css('color', 'red');
// JavaScript
document.querySelectorAll('li:first-child').forEach(el => {
el.style.color = 'red';
});
匹配选择
// jQuery
$('#second ~ li').css('color', 'red');
// JavaScript
document.querySelectorAll('#second ~ li').forEach(el => {
el.style.color = 'red';
});
属性选择
// jQuery
$('[id], [class="first"]').css('color', 'red');
// [attribute!="..."] -> 选择指定属性不等于这个值的元素
// [attribute^="..."] -> 选择指定属性是以给定字符串开始的元素
// [attribute$="..."] -> 选择指定属性是以给定字符串结尾的元素
// [attribute*="..."] -> 选择指定属性具有包含给定字符串的元素
// [attribute="..."][attribute="..."] -> 设置多个指定属性
// JavaScript
document.querySelectorAll('[id], [class="first"]').forEach(el => {
el.style.color = 'red';
});
jQuery自带选择
// jQuery
$('li:first, li:last').css('color', 'red');
$('li:even, li:odd').css('color', 'red');
$('li:lt(2)').css('color', 'red');
$('li:eq(2)').css('color', 'red');
$('li:gt(2)').css('color', 'red');
$(':header').css('color', 'red');
$('li:contains("テキスト")').css('color', 'red');
$('li:has(strong)').css('color', 'red');
$('li:parent').css('color', 'red');
HTML/CSS操作
修改文本
<p id="first">修改前</p> // jQuery
$('p#first').text('Hello Rizu');
// JavaScript
document.querySelector('#first').textContent = 'Hello Rizu'
获取文本
<p id="first">获取的字符串</p>
<p id="second">修改前</p> $('p#second').text($('p#first').text());
// JavaScript
var text = document.querySelector('#first').textContent;
document.querySelector('#second').textContent = text ;
修改html
<p id="first">修改前</p> // jQuery
$('p#first').html('<strong>Hello Rizu</strong>');
// JavaScript
document.querySelector('#first').innerHTML = '<strong>Hello Rizu</strong>';
获取html
<p id="first"><strong>获取的HTML</strong></p>
<p id="second">变更前</p> // jQuery
$('p#second').html($('p#first').html());
// JavaScript
var html = document.querySelector('#first').innerHTML;
document.querySelector('#second').innerHTML = html;
向html元素中的头部、尾部插入
<p id="first">Hello Rizu</p> // jQuery
$('p#first').prepend('<strong>在头部插入</strong>');
$('p#first').append('<strong>在尾部插入</strong>');
// JavaScript
var target = document.querySelector('#first');
var a = document.createElement('strong');
a.textContent = '在头部插入';
var b = document.createElement('strong');
b.textContent = '在尾部插入';
target.insertBefore(a, target.firstChild);
target.appendChild(b);
在html元素的前、后插入
<p id="first">Hello Rizu</p> // jQuery
$('p#first').before('<h1>前面插入</h1>');
$('p#first').after('<h1>后面插入</h1>');
// JavaScript
var before = document.createElement('h1');
var after = document.createElement('h1');
before.textContent = '前面插入';
after.textContent = '后面插入';
var target = document.querySelector('#first');
var parent = target.parentNode;
parent.insertBefore(before, target);
parent.appendChild(after);
向html元素内的头部、尾部移动
<p id="first">Hello Rizu</p>
<strong id="prepend">向头部移动</strong>
<strong id="append">向尾部移动</strong> // jQuery
$('strong#prepend').prependTo('p');
$('strong#append').appendTo('p');
// JavaScript
var prepend = document.querySelector('#prepend');
var append = document.querySelector('#append');
var target = document.querySelector('#first');
target.insertBefore(prepend, target.firstChild);
target.appendChild(append);
在html元素的前、后移动
<strong id="after">向后移动</strong>
<p id="first">Hello Rizu</p>
<strong id="before">向前移动</strong> // jQuery
$('strong#before').insertBefore('p');
$('strong#after').insertAfter('p');
// JavaScript
var before = document.querySelector('#before');
var after = document.querySelector('#after');
var target = document.querySelector('#first');
var parent = target.parentNode;
parent.insertBefore(before, target);
parent.appendChild(after);
使用指定元素包裹各元素
<p id="first">Hello Rizu</p>
<p id="second">Hello yrq</p> // jQuery
$('p').wrap('<h1></h1>');
// JavaScript
var target = document.querySelector('#first');
var parent = target.parentNode;
document.querySelectorAll('p').forEach((value, index) => {
var wrap = document.createElement('h1');
wrap.innerHTML = value.outerHTML;
if (index === 0) { parent.innerHTML = '';}
parent.appendChild(wrap);
});
将所有元素包裹在一个元素中
<div>
<p id="first">Hello Rizu</p>
<p id="second">Hello yrq</p>
</div> // jQuery
$('p').wrapAll('<h1></h1>');
// JavaScript
var target = document.querySelector('div');
var p = target.innerHTML;
var h1 = document.createElement('h1');
h1.innerHTML = p;
target.innerHTML = '';
target.innerHTML = h1.outerHTML;
使用指定元素包裹各元素的子元素
<div>
<p id="first">Hello Rizu</p>
<p id="second">Hello yrq</p>
</div> // jQuery
$('p').wrapInner('<strong></strong');
// JavaScript
var p = document.querySelectorAll('p');
p.forEach((value, index) => {
var target = value.textContent;
var strong = document.createElement('strong');
strong.textContent = target;
p[index].innerHTML = strong.outerHTML;
});
去除父元素
<strong><p id="first">Hello Rizu</p></strong> // jQuery
$('p').unwrap();
// JavaScript
var target = document.querySelector('#first');
var parent = target.parentNode;
var grand = parent.parentNode;
grand.innerHTML = target.outerHTML;
使用其他元素替换指定元素
<p id="first">Hello Rizu</p> // jQuery
$('p').replaceWith('<h1>替换后</h1>');
// JavaScript
document.querySelector('#first').outerHTML = '<h1>替换后</h1>';
删除元素
<p id="first"><strong>删除的元素</strong>Hello Rizu</p> // jQuery
$('p strong').remove();
// JavaScript
var target = document.querySelector('#first');
var nest = document.querySelector('#first strong');
target.removeChild(nest);
获取与修改属性值
<a href="http://yrq110.me/">blog</a> // jQuery
$('a').attr('href', "http://developer.mozilla.org ");
$('a').text($('a').attr('href'));
// JavaScript
document.querySelector('a').setAttribute('href', "http://developer.mozilla.org ");
var target = document.querySelector('a');
target.textContent = target.getAttribute('href');
删除属性值
<a href="http://yrq110.me/" target="_blank">blog</a> // jQuery
$('a').removeAttr('target');
// JavaScript
document.querySelector('a').removeAttribute('target');
class值的添加与删除
<p>Hello Rizu</p> // jQuery
$('p').addClass('red');
$('p').removeClass('red');
// JavaScript
var target = document.querySelector('p');
target.classList.add('red');
target.classList.remove('red');
设置多个CSS属性值(CSS in JS)
<p>Hello Rizu</p> // jQuery
$('p').css({
'color': 'red',
'border': '1px solid black'
});
// JavaScript
document.querySelector('p').style.color = 'red';
document.querySelector('p').style.border = '1px solid black';
点击事件
<p>Hello Rizu</p> // jQuery
$('p').click(function() {
$(this).css('color', 'red');
});
// JavaScript
document.addEventListener('click', () => {
document.querySelector('p').style.color = 'red';
}, false);
jQuery->JavaScript一览表的更多相关文章
- [JS]jQuery,javascript获得网页的高度和宽度
[JS]jQuery,javascript获得网页的高度和宽度网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeigh ...
- jQuery UI 是建立在 jQuery JavaScript 库上的一组用户界面交互、特效、小部件及主题
jQuery UI 是建立在 jQuery JavaScript 库上的一组用户界面交互.特效.小部件及主题.无论您是创建高度交互的 Web 应用程序还是仅仅向窗体控件添加一个日期选择器,jQuery ...
- Visual Studio 2012设置Jquery/Javascript智能提示
Visual Studio 2012设置Jquery/Javascript智能提示 在Visual Studio 2008 Visual Studio 2010中微软已经开始支持jquery/java ...
- [转]Tetris(俄罗斯方块) in jQuery/JavaScript!
本文转自:http://pwwang.com/2009/10/25/tetris-in-jquery-javascript/ All in jQuery/JavaScript + HTML! Demo ...
- jQuery JavaScript Library v3.2.1
/*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...
- jQuery/javascript实现全选全不选
<html> <head> <meta charset="utf-8"> <title>Checkbox的练习</title& ...
- jQuery/javascript实现网页注册的表单验证
<html> <head> <meta charset="utf-8"> <title>注册表单验证</title> & ...
- jQuery/javascript实现简单网页计算器
<html> <head> <meta charset="utf-8"> <title>jQuery实现</title> ...
- JQuery & Javascript
Jquery 是一个优秀的javascript框架,是轻量级的js库 jQuery简化了javascript 编程 jQuery很容易学习
- jquery,javascript -设置某一ul下的li下的 a的属性
//javascriptvar ul = document.getElementById('ul); var as = ul.getElementsByTagName('a'); for(var i ...
随机推荐
- curl模拟带验证码的登录
首先说明,不是用php自动识别验证码,而是有验证码的情况下,让你通过curl 带着cookies去请求远程资源,从而通过合法的身份验证.主要用来抓取需要登录后才能访问的资源. 思路就是获取到验证码之后 ...
- PHP面试随笔
1.常见的HTTP状态码: 1xx系列:代表请求已被接受,需要继续处理 2xx系列:代表请求已成功被服务器接收.理解并接受 200:表示请求已成功,请求所希望的响应头或数据体将随此响应返回 201:表 ...
- PLSQL锁表之后改如何操作
(1)查看哪个表被锁select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects ...
- Servlet编程实例2
上次实验中利用HttpServletRespon.sendRedict()方法来实现页面的转跳,而这种重定向请求的方法无法传递缓存的内容. 所以为了做出改进,这次使用RequestDispatcher ...
- [转载] Redis系统性介绍
转载自http://blog.nosqlfan.com/html/3139.html?ref=rediszt 虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面 ...
- fragmentTabHost 使用示例
目前我们看微信的底部,有四个导航栏,那我们应该来怎么实现类似的导航栏呢? 在 android 4.0 的时候,推出了一个新的工具,fragmentTabHost . fragmentTabHost ...
- [转]Spring.Net介绍
转自:http://www.cnblogs.com/cilence/archive/2013/02/21/2920478.html Spring.NET下载地址:http://www.springfr ...
- Python之Django环境搭建(MAC+pycharm+Django++postgreSQL)
Python之Django环境搭建(MAC+pycharm+Django++postgreSQL) 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7828614. ...
- 自己动手修改Robotium代码(上)
Robotium作为Android自动化测试框架,还有许多不完善的地方,也不能满足测试人员的所有要求.那么,本文以四个实际中碰到的问题为例,介绍改动Robotium源码的过程. public bool ...
- SaltStack 部署案例 02
远程执行 salt '*' state.sls apache '*':代表所有主机 state.sls :是一个模块 apache : 状态 ,表示需要部署的内容,后缀.sls YAML:三板斧 1. ...