您的位置:首页 > Web前端 > JQuery

jquery常用代码段

2016-04-21 10:37 573 查看
1.替换html标签

$('li').replaceWith(function(){

return $("<div />").append($(this).contents());

});


2.检测屏幕宽度

var responsive_viewport = $(window).width();

if (responsive_viewport < 481) {

alert('Viewport is smaller than 481px.');

}


3.检测复制、粘贴与剪切操作

$("#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!')

});


4.自动为外部链接添加target=“blank”属性

var root = location.protocol + '//' + location.host;

$('a').not(':contains(root)').click(function(){

this.target = "_blank";

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: