jQuery方法众多,多练习为上策
案例:
<style>
body, div, ul, li, p {
margin: 0;
padding: 0;
}
body {
color: #666;
font: 12px/1.5 Arial;
}
ul {
list-style-type: none;
}
#star {
position: relative;
width: 600px;
margin: 10px auto;
}
#star ul, #star span {
float: left;
display: inline;
height: 19px;
line-height: 19px;
}
#star ul {
margin: 0 10px;
}
#star li {
float: left;
width: 24px;
cursor: pointer;
text-indent: -9999px;
background: url(http://www.fgm.cc/learn/lesson4/img/star.png) no-repeat;
}
#star strong {
color: #f60;
padding-left: 10px;
}
#star li.on {
background-position: 0 -28px;
}
#star p {
position: absolute;
top: 20px;
width: 159px;
height: 60px;
display: none;
background: url(http://www.fgm.cc/learn/lesson4/img/icon.gif) no-repeat;
padding: 7px 10px 0;
}
#star p em {
color: #f60;
display: block;
font-style: normal;
}
</style>
<div id="star">
<span>点击星星就能打分</span>
<ul>
<li><a href="javascript:;">1</a></li>
<li><a href="javascript:;">2</a></li>
<li><a href="javascript:;">3</a></li>
<li><a href="javascript:;">4</a></li>
<li><a href="javascript:;">5</a></li>
</ul>
<span></span>
<p></p>
</div>
var aMsg = [
"很不满意|差得太离谱,与卖家描述的严重不符,非常不满",
"不满意|部分有破损,与卖家描述的不符,不满意",
"一般|质量一般,没有卖家描述的那么好",
"满意|质量不错,与卖家描述的基本一致,还是挺满意的",
"非常满意|质量非常好,与卖家描述的完全一致,非常满意"
]
$(function () {
$("li").on("mouseenter",function () {
index=$(this).index();
$("li").removeClass("on");
$(this).addClass("on").prevAll().addClass("on");
$("p").html("<em><b>" + (index+1) + "</b> 分 " + aMsg[index].match(/(.+)\|/)[1] + "</em>" + aMsg[index].match(/\|(.+)/)[1]).css({display:"block",left:(index+1)*24});
})
$("li").on("mouseleave",function () {
$(this).removeClass("on").prevAll().removeClass("on");
$(".add").addClass("on").prevAll().addClass("on");
$("p").css("display","none")
})
$("li").on("click",function () {
$(this).addClass("add").nextAll().removeClass("add");
// $("span").eq(1).html($("p").text())
$("span").eq(1).html("<strong>" + (index+1) + "</b> 分 " + aMsg[index].match(/(.+)\|/)[1] + "</strong>" + aMsg[index].match(/\|(.+)/)[1]).css({display:"block",left:(index+1)*24})
})
})
该案例设计on()的注册事件
//解绑事件:off()
$(“p”).on(“click mouseenter”,function () {
alert(“hhe”)
})
// $("p").off()//解绑匹配元素的所有事件
$("p").off("mouseenter")
$("button").on("click",function () {
$("p:last").click();
//触发 trigger(type)
$("p:last").trigger('click')
本次案例方法不多,原理很简单,至于拼接的方法可以详细了解正则表达式