HTML小记十八转义,jQuery小技巧

1.高逼格---转义
反斜杠一般用在转义中,比如在字符串里我们想表示空字符就使用'\b',换行'\n'。同样,在提供给querySelector和querySelectorAll的参数也支持转义,
此时一个小demo 一个class为"tmp:test"的div

    <div class="tmp:test">
            
        </div>

当使用一般的写法时:

document.querySelector('.tmp:test');
报错:
Failed to execute 'querySelector' on 'Document': '.tmp:test' is not a valid selector.

将冒号直接转义就能解决问题么? 下面的就是:

document.querySelector('.tmp\\:test');
还是报错:
Failed to execute 'querySelector' on 'Document': '.tmp\:test' is not a valid selector.

正确的写法;

document.querySelector('.tmp\\:test');

同理 对有反斜杠的div的class="tmp:test";写法是

document.querySelector('.tmp\\\\:test');

一些小的jQuery技巧

2.禁止使用右键点击
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});

  1. 预加载图片
    $(document).ready(function() {
    jQuery.preloadImages = function()
    {
    for(var i = 0; i<ARGUMENTS.LENGTH; jQuery(?<img { i++)>").attr("src", arguments[i]);
    }
    }
    // 用法
    $.preloadImages("image1.jpg");
    });
    4.点击返回到页面顶部

            a{
                display: inline-block;
                margin-top:800px ;
                color: red;
            }
            <a href="#">这个是A标钱</a>

使用jQuery方法:

$(document).ready(function() {
$('a[href*=#]').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
 && location.hostname == this.hostname) {
   var $target = $(this.hash);
   $target = $target.length && $target
   || $('[name=' + this.hash.slice(1) +']');
   if ($target.length) {
  var targetOffset = $target.offset().top;
  $('html,body')
  .animate({scrollTop: targetOffset}, 900);
    return false;
   }
  }
  });
//一些变相的用法:place this where you want to scroll to
<A name=top></A>
// the link
<A href="#top">go to top</A>
});

5.获取鼠标的位置 xy值

$(document).ready(function() {
   $().mousemove(function(e){
     //display the x and y axis values inside the div with the id XY
    $('#XY').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
  });
});
//这里可得到鼠标X坐标
var pointX = e.pageX;
//这里可以得到鼠标Y坐标
var pointY = e.pageY;

6.判断元素是否为空:
if ($('#id').html()) {
// do something
}
7.替换元素
$('#id').replaceWith('
<DIV>I have been replaced</DIV>
');
8.延迟加载的方法 可用于网络请求
$(document).ready(function() {
window.setTimeout(function() {
// do something
}, 1000);
});
9.使元素居中显示

$(document).ready(function() {
  jQuery.fn.center = function () {
      this.css("position","absolute");
      this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
      this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
      return this;
  }
  $("#id").center();
});  

10.统计一个元素的个数,可以统计数组等
$(document).ready(function() {
$("p").size();
});

  1. 解决一些冲突 jQuery类
    $(document).ready(function() {
    var $jq = jQuery.noConflict();
    $jq('#id').show();
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助。其中的一些代码段是...
    forwardRain阅读 450评论 0 3
  • 1.JQuery 基础 改变web开发人员创造搞交互性界面的方式。设计者无需花费时间纠缠JS复杂的高级特性。 1....
    LaBaby_阅读 1,211评论 0 1
  • 1.JQuery 基础 改变web开发人员创造搞交互性界面的方式。设计者无需花费时间纠缠JS复杂的高级特性。 1....
    LaBaby_阅读 1,406评论 0 2
  • 真诚在左,谦卑在右,走在智慧的两侧。随时学习,随时体悟。学完这节我有一种很深刻的体悟,它谈及了我学金刚智慧的缘起。...
    了空_阅读 241评论 0 0
  • 有时候真的觉得自己活得不怎么样,在医院这一切似乎都是过往云烟,活着就是好的
    颓废的小毛驴阅读 153评论 0 0