var a = $('<a>',{ href: url });
console.log(url);
console.log('Host name: ' + a.prop('hostname')); //Host name: tutorialzine.com
console.log('Path: ' + a.prop('pathname')); //Path: /books/jquery-trickshots
console.log('Query: ' + a.prop('search')); //Query: ?trick=12&&abc=123
console.log('Protocol: ' + a.prop('protocol')); //Protocol: http:
console.log('Hash: ' + a.prop('hash')); //Hash: #comments
这样我们就可以很快速的完成URL的分解
9.有时候,缓存selector,反而可以优化你的js
下面有三种情况,第一种情况是一部分人的通常做法,这种写法费力而且不讨好,后面两种方案则是对第一种的优化,可以二选一.
第一种:
$('#pancakes li').eq(0).remove();
$('#pancakes li').eq(1).remove();
$('#pancakes li').eq(2).remove();第二种和第三种,可以二选一:
//第二种
var pancakes = $('#pancakes li');
pancakes.eq(0).remove();
pancakes.eq(1).remove();
pancakes.eq(2).remove();
//第三种
pancakes.eq(0).remove().end().eq(1).remove().end().eq(2).remove().end();
10.on()方法
on() 方法在被选元素及子元素上添加一个或多个事件处理程序。
自 jQuery 版本 1.7 起,on() 方法是 bind()、live() 和 delegate() 方法的新的替代品。该方法给 API 带来很多便利,推荐使用该方法,它简化了 jQuery 代码库。
注意:使用 on() 方法添加的事件处理程序适用于当前及未来的元素(比如由脚本创建的新元素)。
提示:如需移除事件处理程序,请使用 off() 方法。
提示:如需添加只运行一次的事件然后移除,请使用 one 方法。
$(selector).on(event,childSelector,data,function,map)
11.模拟触发事件
我们可以通过trigger模拟触发一个click事件
var press = $('#press');
press.on('click',function(e, how){
how = how || '';
alert('The buton was clicked ' + how + '!');
});
press.trigger('click');
press.trigger('click',['fast']);
同时,我们亦可以,使用on()方法创建自己喜欢的事件名称,然后通过trigger来触发。举例如下:
<button id="button1">Jump</button> <button id="button2">Punch</button> <button id="button3">Click</button> <button id="clear" style="float: right;">Clear</button> <div id="eventDiv"></div>










