常用的jquery方法(更新中)

目錄結構如下:

在這裏插入圖片描述
常用的jquery方法放入utils.js中


方法索引:

  1. 水平垂直居中
  2. 判斷元素是否可見
  3. 正則表達式 替換文本

1.水平垂直居中

不受滾動條影響,始終居中
在這裏插入圖片描述

util中的方法:

// 方法掛載到JQ
// 方法作用:居中
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;
}

html:

	<div class="center"></div>

css:

.center {
	height: 300px;
	width: 600px;
	background-color: #eee;
}

js:

$(function () {
	$('.center').center();
})

2.判斷元素是否可見

js:

if($("div").is(':visible') == true) {
	alert("div可見")
}

3.正則表達式 替換文本

js:

$(function () {
	var exp = /cxk/;
	$(“div”).html(
		$(this).text().replace(exp,"lzx")
	);
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章