jquery优化01
2024-08-25 07:49:24
查找:
- children: find(selector), children(selector);
- parent: parent(), parents(selector), closest(selector) //最近的上层
- find()
- $("#main li");$("#main > li"); ---> $("#main").find("li");$("#main").children('li');
- first()/last()
- $("#main li:first") ---> $("main").find("li").first(); /last()
- filter()
- $("#main.onsale"); ---> $("#main").filter(".onsale");
绑定:
- on(
$("button").click(function(event) {}); ---> $("button").on("click",function() {});
- --->
$("#div").on("click","button",function() {
var message = "<span>click now!</span>";
$(this).after(message).remove();
}); data() //HTML5属性,适用于文本的设置替换
<div data-title="use data">
<button id="set">set</button>
<button id="get">get</button>
</div>
----------------------------------
$("#set").on("click",function() {
$(this).parent().data("title","data has changed");
});
$("#get").on("click",function() {
$(this).after($(this).parent().data("title")).remove();
});- 使用namespace和off()
//of()
-------------------------
$("#btn1").on("click",picture);
$("#btn1").on("click",status);
$("#btn1").off("click"); //namespace
--------------------------
$("#btn2").on("click.pic",picture);
$("#btn2").on("click.stat",status);
$("#btn2").off("click.pic"); $("#btn3").on("click.pic",picture);
$("#btn3").on("click.stat",status);
$("#btn3").on("mouseover.stat",status);
$("#btn3").off(".stat"); - 使用trigger //触发
$("#btn4").on("click",function() {
$("#btn2").trigger("click");
}); $("div").on("exam.new",function() { //自定义
console.log($(this).data());
});
$("#btn5").on("click.pict",function() {
picture();
$("div").trigger("exam.new");
});
after() //与remove一起用节点替换
事件:
- keyboard events: keypress,keydown,keyup;
- form events: focus,blur,change,select,submit;
- mouse events: click,dbclick,focusin,focusout,mousedowm,mouseup,mousemove,mouseout,mouseover,mouseleave,mouseenter;
节点与方法连接
function Confirmation(ele) {
this.ele = ele;
this.ticket = this.ele.find(".ticket");
var confirmation = this;
this.loadConfirmation = function() {
$.ajax("confirmation.html", {
timeoOut: 3000,
context: confirmation,//"this" in the function;
success: function(res) {
this.ticket.html(res).slideDown();
}
});
this.showBoardingPass = function(event) {
event.preventDefault();
$(this).hide();
confirmation.find(".boarding-pass").show();
};
this.ele.on("click","button",this.loadConfirmation);
this.ele.on("click","view-boarding-pass",this.showBoardingPass);
}
}
$(function() {
var paris = new Confirmation($("#paris"));
var london = new Confirmation($("#london"));
});
最新文章
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
- Android 手机卫士--md5加密过程
- android Menu
- Enterprise Library +Caliburn.Micro+WPF CM框架下使用企业库验证,验证某一个属性,整个页面的文本框都变红的原因
- 我认为测试应该掌握的SQL语句
- Tesseract训练笔记
- Windows server 2008 R2远程桌面终端连接数的破解
- posix thread API列表
- (转载)Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
- 解决android启动程序时,会出现一个短暂的白色空白界面的问题
- 自动输入用户名和密码用于telnet的shell
- 【HotSpot】 jps
- python 元祖字典集合
- 如何加入Microsoft Teams 技术社区
- CentOS 7下升级Python版本到3.x系列
- 【待补充】[HDFS_3] HDFS 工作机制
- MathType怎么编辑半开半闭区间
- POJ - 1101 The Game dfs
- python其他知识目录
- js for form