使用:.addClass('active').siblings().removeClass('active');即可
解释:给当前选中的增加边框.addClass('active')
给原先选中的取消边框.siblings().removeClass('active')
详细如下:
<style type="text/css">
.clr:after{clear:bothdisplay:blockoverflow:hiddenheight:0content:"."}
.clr{zoom:1}
.price{width:100%}
.price a{width:100pxheight:40pxline-height:40pxtext-align:centerbackground:#eeefloat:leftmargin:0 5pxdisplay:blockcursor:pointer}
.price a.active{border:1px solid red}
</style>
<div class="price clr">
<a>5元</a>
<a>10元</a>
<a>100元</a>
<a>200元</a>
</div>
<script type="text/javascript" src="引用jquery.js或zepto.js"></script>
<script type="text/javascript">
$(function(){
$('.price a').click(function(){
$(this).addClass('active').siblings().removeClass('active')
})
})
</script>
效果如下:
我写的,支持IE,FF,但删除4后,后续序号还不能变,需要改进,因为本身有工作在身,来不及写。楼主可以先拿去参考,要改成那样不会很难的。可以考虑每次删除后都执行一个重置序号函数。<html>
<head>
<title></title>
<script type="text/javascript">
var item_index=3
function AddItem() {
var myNameList = document.getElementById('divVoteItemPanel')//目标DIV
//创建span用于选项信息
var myNewSpan = document.createElement('span')
var myNewSpanValue = document.createTextNode("选项" + item_index)
//myNewSpan.id = "spanshow" + item_index
//myNewSpan.href = item_index
myNewSpan.appendChild(myNewSpanValue)
//创建input
var myNewInput = document.createElement('input')
myNewInput.name = 'TextVoteItem'
//创建a删除按钮
var myDelete = document.createElement('a')
var myDeleteValue = document.createTextNode('删除')
myDelete.id = "adelete" + item_index
myDelete.href = "#"
myDelete.onclick = Function("deleteID(" + item_index + ")")
myDelete.appendChild(myDeleteValue)
//创建换行容器div
var myDiv = document.createElement('div')
myDiv.id = "amydiv" + item_index
myDiv.appendChild(myNewSpan)//添加span到div
myDiv.appendChild(myNewInput)//添加input到div
myDiv.appendChild(myDelete)//添加a删除按钮到div
//添加div到总div
myNameList.appendChild(myDiv)
item_index++
}
function deleteID(id) {
var amydiv = document.getElementById("amydiv" + id)//获取显示控件
var myNameList = document.getElementById('divVoteItemPanel')//目标DIV
myNameList.removeChild(amydiv)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="98%">
<tr>
<td colspan="3" style="background:#aabbcc">添加投票</td>
</tr>
<tr>
<td>主题:</td>
<td>你认为以下哪个最可爱?</td>
<td style="width:50%"></td>
</tr>
<tr>
<td>
<hr />
<div id="divVoteItemPanel">
<div><span>选项1</span><input type="text" name="TextVoteItem" maxlength="50" /></div>
<div><span>选项2</span><input type="text" name="TextVoteItem" maxlength="50" /></div>
</div>
<div><a href="#" onclick="AddItem()" >添加选项</a></div>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)