jQuery
<script src='jquery.js'></script>
<script>
var mydiv = $('#mydiv');
mydiv.css(width="");
</script>
关系:JQuery的本质是JS,对JS功能的扩展
JQuery与JS的相互转换
-
JS对象 => JQ对象 $(‘JS对象’) -
JQ对象 => JS对象 JQ对象[0]
<script>
var mydiv = document.getElementByid('mydiv');
$(mydiv);
$('#mydiv')[0]
</script>
解决冲突
-
JQuery == $ -
让出$符号 JQuery.noConfilter()
*页面加载完成后
-
$(document).ready(function(){……}) 省略:$().ready(function(){……}) 省略:$(function(){……}) -
与window.onload的区别: onload:表示页面内容全部加载完成 ready:表示页面加载结构全部完成。不包含媒体(如:图片、视频、音频……) ready先执行,onload后执行 ready可以写多次
选择器
注:伪类不适用
<script>
$('ul').css('color','red')
$('li.five').css()
$('#four').css()
$('ul li').css()
$('ul > li').css('color','orange')
$("#woniu + a").css("color", "orange");
$("#woniu ~ a").css("color", "orange");
$('.four').prev('li').css("color", "orange");
$('.four').next('li').css("color", "orange");
$('.four').prevAll('li').css("color", "orange");
$(".four").nextAll("li").css("color", "orange");
$(".four").siblings().css("color", "orange");
$("a:eq(0)").css("color", "orange");
$("li:first").css("color", "orange");
$('li:last').css("color", "orange");
$('li:not(li:last)').css("color", "orange");
$('li:even').css("color", "orange");
$('li:odd').css("color", "orange");
$('li:gt(2)').css("color", "orange");
$("li:lt(2)").css("color", "orange");
</script>
可见性过滤选择器
console.log((li:hidden))
console.log($("body :visible"));
内容过滤选择器
<script>
$('li:contents('我的').css()')
console.log($(div.empty));
$('div:has(span)').css()
$('div:parent').css({
color : red;
})
</script>
边框染色,默认跟着字的颜色变化
属性选择器
<script>
$("a[href]").css('color','red');
$("a[href=mother]").css('background','yellow');
$("a[href!=mother]").css('background','pink');
$("a[href^=http]").css('background','yellowgreen');
$("a[href$=com]").css('font-weight','900');
$("a[href*=o]").css('font-weight','400');
</script>
表单选择器
<script>
console.log($(":input"))
console.log($(":text"));
console.log($(":password"));
console.log($(":radio"));
console.log($(":checkbox"));
console.log($(":submit"));
console.log($(":reset"));
console.log($("button[type=button]"));
console.log($(":file"));
console.log($("input:disabled"));
console.log($("input:enabled"));
console.log($(":checked"));
console.log($(":selected"));
</script>
Each
<script>
$("h1").each(function (index, element) {
console.log(index, element);
});
var arr = [5, 6, 7, 8];
$.each(arr, function (index, element) {
console.log(index, element);
});
</script>
attr
<script>
$("a").attr("title","爱你");
console.log($("a").attr("title"))
$("a").attr({
id:"woniu",
class:"xiao woniu"
})
$("a").removeAttr("class")
</script>
prop
<script>
console.log($("input").attr("checked"));
console.log($("input").attr("disabled"));
console.log($("input").prop("checked"));
console.log($("input").prop("disabled"));
</script>
CSS
<script>
console.log($("div").css("width"));
$("div").css({
backgroundColor: "red",
borderRadius: "20px",
});
console.log($("div").css(["width", "height", "background-color"]));
</script>
Class
<script>
$("div").addClass("a b");
$("div").removeClass("a");
$("div").toggleClass("c");
$("div").toggleClass("c");
</script>
text_val
<script>
$("input").val("大家好");
console.log($("input").val());
console.log($("div").html());
console.log($("div").text());
$("div").html("<b>" + $("div").html() + "</b>");
</script>
Size
<script>
var div = $("div");
console.log(div.width());
console.log(div.innerWidth());
console.log(div.outerWidth());
console.log(div.outerWidth(true));
console.log($(window).height());
console.log($(document).height());
div.mouseover(function(){
$(this).css('background','yellow');
})
</script>
Offset
<script>
console.log($("p").offset().left);
console.log($("p").position().left);
</script>
|