我有两个容器。第一个容器(.links
)具有锚名标记,其类名为block-1, block-2
等...
第二个容器(.highlight-block
)与block-1, block-2
具有相同的类名等...
HTML
<div class="links">
<a href="#" class="lb block-1">Highlight Block 1</a>
<a href="#" class="lb block-2">Highlight Block 2</a>
<a href="#" class="lb block-3">Highlight Block 3</a>
<a href="#" class="lb block-4">Highlight Block 4</a>
<a href="#" class="lb block-5">Highlight Block 5</a>
<a href="#" class="lb block-6">Highlight Block 6</a>
</div>
<div class="highlight-block">
<div class="cb block-1 active">This is Block 1</div>
<div class="cb block-2">This is Block 2</div>
<div class="cb block-3">This is Block 3</div>
<div class="cb block-4">This is Block 4</div>
<div class="cb block-5">This is Block 5</div>
<div class="cb block-6">This is Block 6</div>
</div>
例如:如果我点击<a href="#" class="lb block-2">Highlight Block 2</a>
容器中的.links
... <div class="cb block-2">This is Block 2</div>
应该.active
类
的jQuery
$(document).ready(function () {
$('.links a').click(function () {
$('.highlight-block').find('.cb').toggleClass('active');
});
});
CSS
body {
font-family:arial;
font-size:12px;
}
.links .lb {
margin-left:5px;
}
.highlight-block .cb {
background:#eee;
padding:10px;
border:1px solid #666;
margin:5px;
}
.highlight-block .cb.active {
background:#5cb85c;
border-color:#1E6B1E;
color:#fff;
}
答案 0 :(得分:5)
如果它们的顺序相同,则使用元素的索引而不是常用的类名称会更容易一些:
// bind a click-handler for the <a> elements that are descendants
// of a '.links' element:
$('.links a').click(function () {
// gets the index of the clicked element
// from amongst its siblings:
var i = $(this).index();
// selects the <div> elements that are descendants of
// a '.highlight-block' element:
$('.highlight-block div')
// finds the element that has an index in the collection
// (not amongst its siblings) equal to the index of
// the clicked <a> element:
.eq(i)
// adds the 'active' class-name to that <div>:
.addClass('active')
// selects the sibling elements of the <div>:
.siblings()
// and removes the 'active' class-name:
.removeClass('active');
});
$('.links a').click(function() {
var i = $(this).index();
$('.highlight-block div.cb')
.eq(i)
.addClass('active')
.siblings()
.removeClass('active');
});
.links a {
display: inline-block;
border: 1px solid #000;
margin: 0 0.25em;
}
.active {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="links">
<a href="#" class="lb block-1">Highlight Block 1</a>
<a href="#" class="lb block-2">Highlight Block 2</a>
<a href="#" class="lb block-3">Highlight Block 3</a>
<a href="#" class="lb block-4">Highlight Block 4</a>
<a href="#" class="lb block-5">Highlight Block 5</a>
<a href="#" class="lb block-6">Highlight Block 6</a>
</div>
<div class="highlight-block">
<div class="cb block-1 active">This is Block 1</div>
<div class="cb block-2">This is Block 2</div>
<div class="cb block-3">This is Block 3</div>
<div class="cb block-4">This is Block 4</div>
<div class="cb block-5">This is Block 5</div>
<div class="cb block-6">This is Block 6</div>
</div>
或者,如果您希望允许多个精彩片段处于活动状态:
// selects the <a> element descendants of a '.links'
// element, and binds a click event-handler:
$('.links a').click(function () {
// finds the index of the clicked <a>
// amongst its sibling elements:
var i = $(this).index();
// finds the <div> elements that are descendants of
// a '.highlight-block' element:
$('.highlight-block div')
// finds the <div> element whose index in the
// collection is equal to the index of the <a>:
.eq(i)
// adds the 'active' class-name if it's not present,
// removes the 'active' class-name if it is present:
.toggleClass('active');
});
$('.links a').click(function () {
var i = $(this).index();
$('.highlight-block div')
.eq(i)
.toggleClass('active');
});
.links a {
display: inline-block;
border: 1px solid #000;
margin: 0 0.25em;
}
.active {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="links">
<a href="#" class="lb block-1">Highlight Block 1</a>
<a href="#" class="lb block-2">Highlight Block 2</a>
<a href="#" class="lb block-3">Highlight Block 3</a>
<a href="#" class="lb block-4">Highlight Block 4</a>
<a href="#" class="lb block-5">Highlight Block 5</a>
<a href="#" class="lb block-6">Highlight Block 6</a>
</div>
<div class="highlight-block">
<div class="cb block-1 active">This is Block 1</div>
<div class="cb block-2">This is Block 2</div>
<div class="cb block-3">This is Block 3</div>
<div class="cb block-4">This is Block 4</div>
<div class="cb block-5">This is Block 5</div>
<div class="cb block-6">This is Block 6</div>
</div>
但是,如果你真的想使用相关的类名,那么我可以提供(但不是真的推荐):
// binding a click event-handler to the <a> elements
// which are descendants of a '.links' element:
$('.links a').click(function () {
// getting an Array-like list of class-names from the DOM node,
// and using Function.prototype.call() to allow us to use the
// Array.prototype.slice() in order to convert the Array-like
// list into an actual Array:
var allClasses = Array.prototype.slice.call(this.classList, 0),
// filtering the Array of class-names with Array.prototype.filter():
n = allClasses.filter(function (c) {
// the first argument to the anonymous function ('c')
// is the array-element of the array over which
// we iterate with the filter() method.
// if the following assessment evaluates to true
// the array-element is returned; if it evaluates
// to false it is discarded.
// here we're using RegExp.prototype.test() to
// keep only those array-elements (the String of
// each class-name) which matches a pattern of a
// String 'block-' followed by one or more ('+')
// numbers '\d' and the end-of-string ('$'):
return /block-\d+$/.test(c);
// we then convert that Array to a string:
}).toString()
// and then find the string of digits ('\d'),
// one or more ('+') that end the string ('$'):
.match(/\d+$/);
// because String.prototype.match() returns an Array
// or null we first ensure that there is a returned
// value ('if (n)'):
if (n) {
// selecting the <div> element with the class-name
// of 'block-N' (where 'N' is the found-number):
$('.highlight-block div.block-' + n)
// adding the 'active' class-name to that <div>:
.addClass('active')
// selecting the sibling elements:
.siblings()
// removing the 'active' class-name:
.removeClass('active');
}
});
$('.links a').click(function() {
var allClasses = Array.prototype.slice.call(this.classList, 0),
n = allClasses.filter(function(c) {
return /block-\d+$/.test(c);
}).toString().match(/\d+$/);
if (n) {
$('.highlight-block div.block-' + n)
.addClass('active')
.siblings()
.removeClass('active');
}
});
.links a {
display: inline-block;
border: 1px solid #000;
padding: 0.25em;
margin: 0.2em;
border-radius: 0.5em;
}
.active {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="links"> <a href="#" class="lb block-1">Highlight Block 1</a>
<a href="#" class="lb block-2">Highlight Block 2</a>
<a href="#" class="lb block-3">Highlight Block 3</a>
<a href="#" class="lb block-4">Highlight Block 4</a>
<a href="#" class="lb block-5">Highlight Block 5</a>
<a href="#" class="lb block-6">Highlight Block 6</a>
</div>
<div class="highlight-block">
<div class="cb block-1 active">This is Block 1</div>
<div class="cb block-2">This is Block 2</div>
<div class="cb block-3">This is Block 3</div>
<div class="cb block-4">This is Block 4</div>
<div class="cb block-5">This is Block 5</div>
<div class="cb block-6">This is Block 6</div>
</div>
虽然简单地找到点击的<a>
元素的相关类名实际上更容易,但只是使用它(而不是上面的方法,我错过了类名{{1}的事实是一样的:
'block-n'
// finding the <a> element descendants of '.links' elements, and
// binding a click event-handler:
$('.links a').click(function () {
// converting the Array-like list of class-names to an Array:
var allClasses = Array.prototype.slice.call(this.classList, 0),
// using the Array.prototype.filter() Array method
// to find those array-elements (the class-names)
// that have a string of 'block-' followed by a
// numeric character ('\d') repeated one or more times
// ('+') followed by the end of string ('$'):
blockClass = allClasses.filter(function (c) {
return /block-\d+$/.test(c);
// converting the Array to a string:
}).toString();
// finding the <div> element descendants of a
// '.highlight-block' element whose class-name
// (the descendant <div>) is equal to the class
// -name we found above from the clicked <a>:
$('.highlight-block div.' + blockClass)
// adding the 'active' class-name:
.addClass('active')
// finding the siblings of the <div>:
.siblings()
// removing the 'active' class-name:
.removeClass('active');;
});
$('.links a').click(function() {
var allClasses = Array.prototype.slice.call(this.classList, 0),
blockClass = allClasses.filter(function(c) {
return /block-\d+$/.test(c);
}).toString();
$('.highlight-block div.' + blockClass)
.addClass('active')
.siblings()
.removeClass('active');;
});
.links a {
display: inline-block;
border: 1px solid #000;
padding: 0.25em;
margin: 0.2em;
border-radius: 0.5em;
}
.active {
background-color: red;
}
外部JS Fiddle。
参考文献:
答案 1 :(得分:0)
如果你想要它很难:
$(document).ready(function () {
$('.links a').click(function () {
class_is=$(this).attr('class').match(/block-[0-9]/,'');
$('.highlight-block div').each(function() {
if($(this).attr('class').indexOf(class_is)!==-1) {
$(this).toggleClass('active');
$(this).siblings().removeClass('active');
}
})
});
});
演示:http://jsfiddle.net/hurwpea3/4/
但是,我真的建议使用自定义数据属性方法或索引方法(如果html结构总是如上所述)。
答案 2 :(得分:0)
改变你的jQuery:
$(document).ready(function () {
$('.links a').on('click', function () {
className = $(this).attr('class').split(' ')[1];
$('.highlight-block').find('.cb.' + className).toggleClass('active');
});
});
这是Fiddle
您也可以使用event
对象中的传递给您的函数并致电e.target
而不是使用this
,如下所示:
$(document).ready(function () {
$('.links a').on('click', function (e) {
className = $(e.target).attr('class').split(' ')[1];
$('.highlight-block').find('.cb.' + className).toggleClass('active');
});
});