Jquery 显示仅单击的元素的文本
我需要能够单击一个div,显示其中的文本,然后在单击另一个div时,隐藏其他div中的所有其他文本,并显示单击的div中的文本 代码在这里Jquery 显示仅单击的元素的文本,jquery,Jquery,我需要能够单击一个div,显示其中的文本,然后在单击另一个div时,隐藏其他div中的所有其他文本,并显示单击的div中的文本 代码在这里 首先 第二 第三 .myClass{ 高度:50px; 宽度:100%; 背景色:红色; 保证金:2倍; } .myClass span{ 显示:无; } $(“.myClass”)。单击(函数(){ //隐藏所有项目范围 $(“.myClass span”).hide(); //显示该元素旁边的元素 $(this.first().show(); });
首先
第二
第三
.myClass{
高度:50px;
宽度:100%;
背景色:红色;
保证金:2倍;
}
.myClass span{
显示:无;
}
$(“.myClass”)。单击(函数(){
//隐藏所有项目范围
$(“.myClass span”).hide();
//显示该元素旁边的元素
$(this.first().show();
});
这将起作用:
您需要使用
.find('span')
获取子span:
$(this).find('span').show();
你是说手风琴。。看看这个。这很容易 或者如果通过EGH代码
$(".myClass").click(function() {
$(".myClass span").css('display','none');
$(this).find('span').css('display','block');
});
使用jQuery函数从隐藏过程中排除要显示的项
$(".myClass").on('click', function () {
// Store a reference to the element we want to show
var shownElement = $(this).find('span');
// Ensure 'shownElement' is shown
shownElement.show();
// Hide everything apart from 'shownElement'
$(".myClass span").not(shownElement).hide();
});
use .find in Jquery; the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements.
$(this).find('span').show();
or .has() in jquery Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.
$(this).has('span').show();
$(".myClass").click(function() {
$(".myClass span").css('display','none');
$(this).find('span').css('display','block');
});
$(".myClass").on('click', function () {
// Store a reference to the element we want to show
var shownElement = $(this).find('span');
// Ensure 'shownElement' is shown
shownElement.show();
// Hide everything apart from 'shownElement'
$(".myClass span").not(shownElement).hide();
});