首页 / JQUERY / jQuery简便使用(上)
jQuery简便使用(上)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jQuery简便使用(上),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5139字,纯文字阅读大概需要8分钟。
内容图文
1.为什么要使用jQuery?
jQuery最近比较流性,由于其优秀的设计和架构,使得开发更容易和快捷了。以下可以认为是jQuery的有点有参考价值:
1.jQuery代码的独特写法
2.jQuery可以和JavaScript共存融合
3.强大的网页元素操作效率
4.对浏览器兼容性非常友好和大量的工具集
2.取值和赋值和遍历操作
<!DOCTYPE html>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>取值和遍历</title><scriptsrc="../jquery-1.8.3.js"></script><scripttype="text/javascript">
$(function(){//val()没有参数表示获取值,有参数表示设置值
alert($(‘input‘).val());
$(‘input‘).val(‘Hello jQuery!!!‘);
//第一个参数表示元素索引值,第二个参数表示元素对象本身
$(‘li‘).each(function(index,elem){
$(elem).html(index + 1);
});
});
</script></head><body><inputtype="button"value="按钮"><ul><li></li><li></li><li></li></ul></body></html>
3.属性操作
<!DOCTYPE html>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>5_jQuery中的属性操作</title><scriptsrc="../jquery-1.8.3.js"></script><styletype="text/css">div{width:50px;height:50px;background: yellowgreen;padding:60px;border:70px solid red;}.box1{background: yellow;}.box2{background: blue;}</style><scripttype="text/javascript">
$(function(){//$(‘div‘).addClass(‘.box1 .box2‘);//打开浏览器调试工具才能看到
alert($(‘.box‘).width());//width 50
alert($(‘.box‘).innerWidth());//width + padding 50+60+60=170
alert($(‘.box‘).outerWidth());//width + padding + border 50+60+60+70+70=310
});
</script></head><body><divclass="box">jQuery</div></body></html>
4.DOM操作
<!DOCTYPE html>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>6_jQuery DOM操作</title><scriptsrc="../jquery-1.8.3.js"></script><styletype="text/css">body{height:2000px;}div{width:100px;height:100px;background: greenyellow;}p{background: gray;}span{background: seagreen;}</style><scripttype="text/javascript">
$(function(){
$(‘span‘).insertBefore($(‘div‘));//对dom节点的剪切移动并非复制
$(‘span‘).remove();//移除span//on方法里面还可以调用自定义方法非常方便使用
$(‘div‘).on({
‘click‘:function() {
alert(‘jQuery‘);
},
‘mouseover‘:function(){
alert(‘Bootstrap‘);
}
});
//off方法是阻止某个方法的执行
$(‘div‘).on(‘click mouseover‘,function(){
alert(‘jQuery‘);
$(‘div‘).off(‘click‘);
});
$(document).click(function(){
alert( $(window).scrollTop() );//滚动的高度
});
});
</script></head><body><div>jQuery</div><p>Bootstrap</p><span>HTML5</span></body></html>
5.事件细节
<scripttype="text/javascript">
$(function(){
$(‘#box‘).click(function(ev){//ev:event对象,已经处理过的兼容事件对象 这里的ev相当于JavaScript中的var ev = ev || window.event ;//alert(ev.pageX);//ev.pageX是相对于文档的//clientX是JavaScript原生方法相对于可视区//ev.whick:keyCode 键盘事件//ev.preventDefault();//阻止默认事件//ev.stopPropagation();//阻止冒泡事件returnfalse ;//阻止默认事件和冒泡事件
});
});
</script>
6.动画(案例)
<!DOCTYPE html>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>动画</title><styletype="text/css">body,ul,li,div,a{margin:0;padding:0;}#menu{margin:0 auto;}#box{width:120px;height:36px;background:#ECE8E7;line-height:36px;text-align: center;}#win{width:120px;height:100px;}a{text-decoration: none;}a:hover{background: pink;display: block;}ul{width:120px;background:#F8F8F8;}li{list-style: none;height:36px;line-height:36px;font-size:18px;text-align: center;
border:1px solid #ECE8E7;color:#888888;}</style><scriptsrc="../jquery-1.8.3.js"></script><scripttype="text/javascript">
$(function(){
$(‘#box‘).hover(function(){//$(this).css(‘background‘,‘blue‘);//$(‘#win‘).hide();//$(‘#win‘).fadeOut(1000);
$(‘#win‘).slideDown(1000);
},function(){//$(this).css(‘background‘,‘pink‘);//$(‘#win‘).show();//$(‘#win‘).fadeIn(1000);
$(‘#win‘).slideUp(1000);
});
});
</script></head><body><divid="menu"><divid="box">WEB前端开发</div><divid="win"><ul><li><ahref="#">jQuery</a></li><li><ahref="#">JavaScript</a></li><li><ahref="#">Bootstrap</a></li><li><ahref="#">AngularJS</a></li><li><ahref="#">Node JS</a></li></ul></div></div></body></html>
7.案例:固定的导航栏
思路:当滚动条滚动的时候动态设置导航栏的位置
<!DOCTYPE html>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>固定导航栏</title><styletype="text/css">body,div{padding:0;margin:0;}#box{width:100%;height:150px;background: gainsboro;position: absolute;}</style><scriptsrc="../jquery-1.8.3.js"></script><scripttype="text/javascript">/*
当滚动滚动条的时候顶部导航栏不变
思路:当触发滚动事件的时候重新计算div的top值
*/
$(function(){
$(window).on(‘resize scroll‘,function(){
$(‘#box‘).css(‘left‘,0);
$(‘#box‘).css(‘top‘, $(window).scrollTop());
});
});
</script></head><bodystyle="height: 2000px;"><divid="box"></div></body></html>
原文:http://blog.csdn.net/unikylin/article/details/44502823
内容总结
以上是互联网集市为您收集整理的jQuery简便使用(上)全部内容,希望文章能够帮你解决jQuery简便使用(上)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】