each()遍历元素(k1)
$(document).ready(function () {
$("#btn").html("each()遍历元素").click(function (event) {
$("div").each(function (index) {
$(this).html("这是第" + index + "个div");
});
event.preventDefault();
});
});
$(document).ready(function () {
$("#btn").html("获取属性值").click(function (event) {
$("div").each(function () {
alert("title属性的值是:"+$(this).attr("title"));
});
event.preventDefault();
});
});
$(document).ready(function () {
$("div").each(function () {
$(this).html(this.title);
});
$("#btn").html("设置属性值").click(function (event) {
$("div").each(function () {
$(this).attr("style", "color:Red");
});
event.preventDefault();
});
});
$(document).ready(function () {
$("div").each(function () {
$(this).html(this.title);
});
$("#btn").html("设置属性值").click(function (event) {
$("div").each(function (index) {
$(this).attr("id", function () {
return "div-id" + index;
}).html($(this).attr("id"));
});
event.preventDefault();
});
});
addClass(names),removeClass(names),toggleClass(names)
$(document).ready(function () {
$("div").each(function () {
$(this).html(this.title).addClass("myClass1").mouseover(function () {
$(this).toggleClass("myClass2");
});
});
});
$(document).ready(function () {
$("div").each(function () {
$(this).html(this.title).css({ color: "Red", opacity: "0.5" }).mouseover(function () {
$(this).css("opacity", "1.0");
}).mouseout(function () {
$(this).css("opacity", "0.5");
});
});
});
$(document).ready(function () {
$("p").append($("a:eq(0)"));
$("p:eq(1)").append($("a:eq(1)"));
});
$(function () {
$("p").remove(":contains(P)");// 等同于$("p:contains("P")").remove();
});
$(function () {
$("p").css({border:"1px solid #FF0000",height:"20px"}).empty();
});
$(function () {
$("#btn-k3").html("clone()克隆自己并克隆事件").click(function () {
//克隆自己并克隆单击事件(设为true)
$(this).clone(true).insertAfter(this);
});
});
$(function () {
$("input[type=button]").click(function () {
var sValue = $(this).val();
$("input[type=text]").val(sValue);
});
});
$(function () {
for (var i = 0; i < 10; i++) {
$("div:last").clone(true).insertAfter($("div:last"));
};
$("div").one("click", function () {
$(this).addClass("myClass1").html("只能点一次");
});
});
$(function () {
$("div").clone().html("unbind()删除事件").click(function () {
$("div").unbind();
}).insertAfter($("div"));
$("div:first").click(function () {
alert("未删除事件");
});
});
$(function () {
$("div").click(function () {
alert("单击事件");
});
$(".myClass3").trigger("click");
});
$(function () {
$("img").attr("title","toggle()实现单击事件的动态交替").toggle(function (event) {
$(event.target).attr("src", "Img/Img2.jpg");
},
function (event) {
$(event.target).attr("src", "Img/Img1.jpg");
});
});
$(function () {
$("img").hover(function (event) {
$(event.target).css("opacity", "1.0");
},
function (event) {
$(event.target).css("opacity", "0.5");
}
);
});
<%--k1--%>
<form id="form1" runat="server">
<div id="1" title="iPhone"></div>
<div id="2" title="Lumia900"></div>
<div id="3" title="HTC"></div>
<button id="btn"></button>
</form>
<%--k2--%>
<a href="#">要被添加的链接1</a>
<a href="#">要被添加的链接2</a>
<p>iPhone</p>
<p>Lumia900</p>
<%--k3--%>
<button id="btn-k3"></button>
<%--k4--%>
<input type="button" value="iPhone5" />
<input type="button" value="Lumia900" />
<input type="button" value="HTC" />
<input type="text" />
<%--k5--%>
<div class="myClass3">点击我</div>
<%--k6--%>
<img alt="美图" src="Img/Img1.jpg"/>
.myClass1{ color:Blue; background:#e58302;}
.myClass2{ color:Red;}
.myClass3{ border:1px solid #FF0000; height:50px; width:80px; float:left;}