iframe相关
在iframe外层获取iframe内层的元素
$("#iframe的ID").contents().find("#iframe中的控件ID").click();
$("#ifm").contents().find("#btnOk").click();
$("#iframe中的控件ID",document.frames("frame的name").document).click();
$("#btnOk",document.frames("ifm").document).click();
在iframe内层获取iframe外层的元素
$('#父窗口中的元素ID', parent.document).click();
$('#btnOk', parent.document).click();
$('#同级iframe元素ID', parent.frames("frame的name").document).click();
$('#btnOk', parent.frames("ifm").document).click();
在iframe内层获取当前iframe元素的id
var frameId = window.frameElement && window.frameElement.id || '';
选择器
元素选择器
$("p")
id选择器
$("#test")
class选择器
$(".test")
$("*")
$(this)
$("p.intro")
伪类选择器
$("p:first")
$("ul li:first-child")
$(":button")
$("tr:even")
$("tr:odd")
属性选择器
$("[href]")
$("a[target='_blank']")
$("a[target!='_blank']")
查找元素
获取祖先元素
parent()
parents()
parentsUntil()
子孙元素
children()
find()
同胞元素
siblings()
next()
nextAll()
nextUntil()
prev()
prevAll()
prevUntil()
特殊
first()
last()
eq()
filter()
not()
dom操作
获取/设置内容
text() - 设置或返回所选元素的文本内容
html() - 设置或返回所选元素的内容(包括 HTML 标记)
val() - 设置或返回表单字段的值
attr() 设置或获取属性值
prop() 设置或获取dom本身的属性值
添加元素 (可以添加纯文本或者元素)
append() - 在被选元素的结尾插入内容
prepend() - 在被选元素的开头插入内容
after() - 在被选元素之后插入内容
before() - 在被选元素之前插入内容
var txt1="<b>I </b>";
var txt2=$("<i></i>").text("love ");
var txt3=document.createElement("big");
txt3.innerHTML="jQuery!";
$("img").after(txt1,txt2,txt3);
删除元素
remove() - 删除被选元素(及其子元素)
empty() - 从被选元素中删除子元素(不删除本身)
css操作
addClass() - 向被选元素添加一个或多个类
removeClass() - 从被选元素删除一个或多个类
toggleClass() - 对被选元素进行添加/删除类的切换操作
css() - 设置或返回样式属性
$("p").css({"background-color":"yellow","font-size":"200%"});
尺寸
width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。
height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。
innerWidth() 方法返回元素的宽度(包括内边距)。
innerHeight() 方法返回元素的高度(包括内边距)。
outerWidth() 方法返回元素的宽度(包括内边距和边框)。
outerHeight() 方法返回元素的高度(包括内边距和边框)。
效果/动画
隐藏和显示
hide()
show()
toggle()
淡入淡出
fadeIn()
fadeOut()
fadeToggle()
fadeTo()
滑动
slideDown()
slideUp()
slideToggle()
自定义动画
$(selector).animate({params},speed,callback);
$("div").animate({left:'250px'});
$("button").click(function(){
$("div").animate({
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
停止动画
$(selector).stop(stopAll,goToEnd);
时间
speed 或 duration 参数可以设置许多不同的值,比如 "slow", "fast", "normal" 或毫秒。
$("p").hide(1000);
回调函数
$("button").click(function(){
$("p").hide("slow",function(){
alert("段落现在被隐藏了");
});
});
jquery链
通过 jQuery,可以把动作/方法链接在一起。
Chaining 允许我们在一条语句中运行多个 jQuery 方法(在相同的元素上)。
$("#p1").css("color","red").slideUp(2000).sblurlideDown(2000);
$("#p1").css("color","red")
.slideUp(2000)
.slideDown(2000);
事件
鼠标
click()
dblclick()
mouseenter()
mouseleave()
hover()
键盘
keypress()
keydown()
keyup()
表单
submit()
change()
focus()
blur()
文档/窗口
```javascript
load()
unload()
resize()
scroll()
ajax
load 从服务器加载数据,并把返回的数据放入被选元素中。
$(selector).load(URL,data,callback);
$("button").click(function(){
$("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
if(statusTxt=="success")
alert("外部内容加载成功!");
if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusText);
});
});
get 通过 HTTP GET 请求从服务器上请求数据。
$.get(URL,callback);
$("button").click(function(){
$.get("demo_test.php",function(data,status){
alert("数据: " + data + "\n状态: " + status);
});
});
post 通过 HTTP POST 请求向服务器提交数据。
$.post(URL,data,callback);
$("button").click(function(){
$.post("/try/ajax/demo_test_post.php",
{
name:"菜鸟教程",
url:"http://www.runoob.com"
},
function(data,status){
alert("数据: \n" + data + "\n状态: " + status);
});
});
解决冲突
noConflict() 方法会释放对 $ 标识符的控制,这样其他脚本就可以使用它了。
$.noConflict();
jQuery(document).ready(function(){
jQuery("button").click(function(){
jQuery("p").text("jQuery 仍然在工作!");
});
});
var jq = $.noConflict();
jq(document).ready(function(){
jq("button").click(function(){
jq("p").text("jQuery 仍然在工作!");
});
});
$.noConflict();
jQuery(document).ready(function($){
$("button").click(function(){
$("p").text("jQuery 仍然在工作!");
});
});