我怎样才能只用一个元素呢? [英] How can I make this for only one element?
本文介绍了我怎样才能只用一个元素呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码适用于页面中的所有范围.我应该将this
放在哪里?
This code works for all the spans in the page. Where should I put this
for the specific one?
var next = function(e) {
var current = $('.active');
var prev = $('#prev');
pos = $('.active').attr('id');
$("#num").text('(' + pos + '/' + researchPlaces.length + ')');
$(current).next().attr("class", "active");
$(current).attr("class", "passive");
e.stopPropagation();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="address"><a id="prev">Prev </a>
<span id="address" class="active">0 Elgin Street</span>
<span id="address" class="passive">1 Elgin Street</span>
<span id="address" class="passive">2 Elgin Street</span>
<span id="address" class="passive">3 Elgin Street</span>
<span id="address" class="passive">4 Elgin Street</span>
<span id="address" class="passive">5 Elgin Street</span>
<span id="address" class="passive">6 Elgin Street</span>
<span id="address" class="passive">7 Elgin Street</span>
<span id="address" class="passive">8 Elgin Street</span>
<span id="address" class="passive">9 Elgin Street</span>
<a id="next"> Next</a>
</div>
推荐答案
测试此;):
<html>
<head>
<title>this is a test</title>
<meta content="">
<style></style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var prev = $("#prev");
var next = $("#next");
var cur = $("#address .active"); /* just spans that in the #address ! */
var curNum = parseInt(cur.text().match(/\d+/));
/* ############ the next function #############*/
next.click(function(){
if(cur.next().attr("id") != "next"){
curNum += 1;
$("#num").text(curNum);
cur.attr("class","addy passive");
cur = cur.next();
cur.attr("class","addy active");
}
});
/* ########### the prev function ###############*/
prev.click(function(){
if(cur.prev().attr("id") != "prev"){
curNum -= 1;
$("#num").text(curNum);
cur.attr("class","addy passive");
cur = cur.prev();
cur.attr("class","addy active");
}
});
});
</script>
</head>
<body>
<div id="num">1</div>
<div id="address"> <a id="prev" data-dir="prev">Prev </a>
<span class="addy active">0 Elgin Street</span>
<span class="addy passive">1 Elgin Street</span>
<span class="addy passive">2 Elgin Street</span>
<span class="addy passive">3 Elgin Street</span>
<span class="addy passive">4 Elgin Street</span>
<span class="addy passive">5 Elgin Street</span>
<span class="addy passive">6 Elgin Street</span>
<span class="addy passive">7 Elgin Street</span>
<span class="addy passive">8 Elgin Street</span>
<span class="addy passive">9 Elgin Street</span>
<a id="next" data-dir="next"> Next</a>
</div>
</body>
</html>
这篇关于我怎样才能只用一个元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文