$(document).ready(function() {
if (navigator.userAgent.match(/msie/i) ){
alert('I am an old fashioned Internet Explorer');
}
});
$("a[href='#top']").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
$(function(){
var $win = $(window)
var $nav = $('.mytoolbar');
var navTop = $('.mytoolbar').length && $('.mytoolbar').offset().top;
var isFixed=0;
processScroll()
$win.on('scroll', processScroll)
function processScroll() {
var i, scrollTop = $win.scrollTop()
if (scrollTop >= navTop && !isFixed) {
isFixed = 1
$nav.addClass('subnav-fixed')
} else if (scrollTop <= navTop && isFixed) {
isFixed = 0
$nav.removeClass('subnav-fixed')
}
}
$('li').replaceWith(function(){
return $("<div />").append($(this).contents());
});
var responsive_viewport = $(window).width();
/* if is below 481px */
if (responsive_viewport < 481) {
alert('Viewport is smaller than 481px.');
} /* end smallest screen */
$('img').error(function(){
$(this).attr('src', 'img/broken.png');
});
$("#textA").bind('copy', function() {
$('span').text('copy behaviour detected!')
});
$("#textA").bind('paste', function() {
$('span').text('paste behaviour detected!')
});
$("#textA").bind('cut', function() {
$('span').text('cut behaviour detected!')
});
在链接至外部站点时,大家可能希望使用target=”blank”属性以确保在新的选项卡中打开页面。问题在于,target=”blank”属性并未经过W3C认证。jQuery能够帮上大忙:以下片段能够检测当前链接是否指向外部,如果是则自动为其添加target=”blank”属性。
var root = location.protocol + '//' + location.host;
$('a').not(':contains(root)').click(function(){
this.target = "_blank";
});
$(document).ready(function(){
$(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads
$(".thumbs img").hover(function(){
$(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
},function(){
$(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout
});
});
$('input.nospace').keydown(function(e) {
if (e.keyCode == 32) {
return false;
}
});
注意:Jquery中用offset().top和offsetTop的比较
offsetTop:当前对象到其上级层顶部的距离。不能对其进行赋值.设置对象到页面顶部的距离请用style.top属性。
offset().top:指元素与document的上边的距离,而不是浏览器当前窗体的上边缘。
jQuery为开发插件提拱了两个方法,分别是:
jQuery.fn.extend();
jQuery.extend();
虽然 javascript 没有明确的类的概念,但是用类来理解它,会更方便。
jQuery便是一个封装得非常好的类,比如我们用 语句 $(“#btn1”) 会生成一个 jQuery类的实例。
jQuery.extend(object);为jQuery类添加类方法,可以理解为添加静态方法。如:
jQuery.extend({
min: function(a, b) { return a < b ? a : b; },
max: function(a, b) { return a > b ? a : b; }
});
jQuery.min(2,3); // 2
jQuery.max(4,5); // 5
jQuery.extend( target, object1, [objectN]) 用一个或多个其他对象来扩展一个对象,返回被扩展的对象。 var settings = { validate: false, limit: 5, name: “foo” }; var options = { validate: true, name: “bar” }; jQuery.extend(settings, options); 结果:settings == { validate: true, limit: 5, name: “bar” }
jQuery.fn.extend(object); 对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”。jQuery类的实例可以使用这个“成员函数”。
比如我们要开发一个插件,做一个特殊的编辑框,当它被点击时,便alert 当前编辑框里的内容。可以这么做:
$.fn.extend({
alertWhileClick:function() {
$(this).click(function(){
alert($(this).val());
});
}
});
// 页面上为:$("#input1") 为一个jQuery实例,当它调用成员方法 //alertWhileClick后,便实现了扩展,每次被点击时它会先弹出目前编辑里的内容。
$("#input1").alertWhileClick();
.fn是指jquery的命名空间,加上fn上的方法及属性,会对每一个jquery实例有效。如扩展 .fn.abc()
那么你可以这样子:$(“#div”).abc(); 通常使用jQuery.extend()方法扩展.
$.fn是什么东西呢。查看jQuery代码,就不难发现。
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {//….
};
原来 jQuery.fn = jQuery.prototype.对prototype肯定不会陌生啦。
jquery源码分析:http://www.admin10000.com/document/7067.html,看过源码上面的内容就很好理解了。
你是否看到过这样的代码:a=a||”“; 可能javascript初学者会对此感到茫然。今天就跟大家分享一下我的一些心得。 其实:
a=a||”defaultValue”;
与:
if(!a){
a="defaultValue";
}
和:
if(a==null||a==""||a==undefined){
a="defaultValue";
}
是等价的! 为了弄清这个问题,首先我们必须了解一个问题:javascript中数据类型在转换为bool类型时发生了什么。
在javascript中,数据类型可以分为“真值”和“假值”。顾名思义,真值转换为bool时值为true;假值转换为bool时值为false。下表罗列了一些常见的数据类型转换为bool时的值:
数据类型 | 转换为bool后的值 |
---|---|
undefined | false |
Object | true |
function | true |
0 | false |
1 | true |
0/1之间的数 | true |
字符串 | true |
“”(空字符串) | false |
在if表达式中,javascript首先将条件表达式转换为bool类型,表达式为真值则执行if中的逻辑,否则跳过。
于是有了:
if(!a){
a=”defaultValue”;
}
下面我们再来看“&&”、“||”两个表达式。
由于javascript是弱类型语言,所以在javascript中这两个表达式可能跟其他语言(比如java)中不太一样。
在javascript中,“&&”运算符运算法则如下:
如果&&左侧表达式的值为真值,则返回右侧表达式的值;否则返回左侧表达式的值。
这就是说:
var i=”“&&”真值”; //->i=””
i=”真值”&&”其他真值”; //->i=”其他真值”
i=”真值”&&”“; //->i=””
“||”运算符的运算法则如下:
如果||左侧表达式的值为真值,则返回左侧表达式的值;否则返回右侧表达式的值。
这就是说:
var i=”“||”真值”;//->i=”真值”
i=”真值”||”其他真值”;//->i=”真值”
i=”真值”||”“;//->i=”真值”
于是,就可以理解:a=a||”defaultValue”;的逻辑了。如果a为假值(等于null、空字符串……),则将”defaultValue”赋给a;否则将a的值赋给a本身。