Javascript jquery-将toggleClass限制为2个元素 $(文档).ready(函数(){ $('#mytable td')。单击(函数(){ $(this.toggleClass('newclass'); }); });
此函数用于更改我的表中选定列的类。它很好用 但是,它不允许同时选择2个以上的列(我所说的selected是指将new类切换为-newclass的列)。因此,如果单击其他列,它就不应该做出反应 我该怎么做?我可以计算当前有多少列使用newclass,如果是2,则取消该函数。我不确定如何计算它们,或者这是否是最好的方法。该方法显示有多少其他元素与该选择器匹配,因此您可以在应用toggleClass()之前进行测试:Javascript jquery-将toggleClass限制为2个元素 $(文档).ready(函数(){ $('#mytable td')。单击(函数(){ $(this.toggleClass('newclass'); }); });,javascript,jquery,Javascript,Jquery,此函数用于更改我的表中选定列的类。它很好用 但是,它不允许同时选择2个以上的列(我所说的selected是指将new类切换为-newclass的列)。因此,如果单击其他列,它就不应该做出反应 我该怎么做?我可以计算当前有多少列使用newclass,如果是2,则取消该函数。我不确定如何计算它们,或者这是否是最好的方法。该方法显示有多少其他元素与该选择器匹配,因此您可以在应用toggleClass()之前进行测试: $(文档).ready(函数(){ $('#mytable td')。单击(函数(
$(文档).ready(函数(){
$('#mytable td')。单击(函数(){
if($(“#mytable td.newclass”).size()<2)
{
$(this.toggleClass('newclass');
}
});
});
该方法将显示有多少其他元素与该选择器匹配,因此您可以在应用toggleClass()之前对其进行测试:
$(文档).ready(函数(){
$('#mytable td')。单击(函数(){
if($(“#mytable td.newclass”).size()<2)
{
$(this.toggleClass('newclass');
}
});
});
尝试使用如下属性:
<script type="text/javascript">
$(document).ready(function () {
$('#mytable td').click(function () {
if ($("#mytable td.newclass").size() < 2)
{
$(this).toggleClass('newclass');
}
});
});
</script>
$(文档).ready(函数(){
$('#mytable td')。单击(函数(){
if($('#mytable td.newclass').length<2)){
$(this.toggleClass('newclass');
}
});
});
希望这有帮助 尝试使用如下属性:
<script type="text/javascript">
$(document).ready(function () {
$('#mytable td').click(function () {
if ($("#mytable td.newclass").size() < 2)
{
$(this).toggleClass('newclass');
}
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#mytable td').click(function () {
if ($('#mytable td.newclass').length < 2)){
$(this).toggleClass('newclass');
}
});
});
</script>
$(文档).ready(函数(){
$('#mytable td')。单击(函数(){
if($('#mytable td.newclass').length<2)){
$(this.toggleClass('newclass');
}
});
});
希望这有帮助
<script type="text/javascript">
$(document).ready(function () {
$('#mytable td').click(function () {
if ($('#mytable td.newclass').length < 2)){
$(this).toggleClass('newclass');
}
});
});
</script>
$(文档).ready(函数(){
$('#mytable td')。单击(函数(){
if(!$(this).hasClass('newClass')){
if($((“td.newClass”).size()<2){
$(this.toggleClass('newclass');
}
}
否则{
$(this.toggleClass('newclass');
}
});
});
$(文档).ready(函数(){
$('#mytable td')。单击(函数(){
if(!$(this).hasClass('newClass')){
if($((“td.newClass”).size()<2){
$(this.toggleClass('newclass');
}
}
否则{
$(this.toggleClass('newclass');
}
});
});
这里有一种方法可以确保不超过2个
$('#mytable td')。单击(函数(){
var$this=$(this);
//如果需要,则切换“开”
//1.它还没有课程,而且
//2.设置为.newclass的数量少于两个
//如果需要,请切换“关闭”
//1.它已经有了类和
//2.设置为.newclass的数量少于两个
$this.toggleClass('newclass',
!$this.hasClass('newclass')&&
$('#mytable.newclass')。长度<2);
})
这里有一种方法可以确保不超过2个
$('#mytable td')。单击(函数(){
var$this=$(this);
//如果需要,则切换“开”
//1.它还没有课程,而且
//2.设置为.newclass的数量少于两个
//如果需要,请切换“关闭”
//1.它已经有了类和
//2.设置为.newclass的数量少于两个
$this.toggleClass('newclass',
!$this.hasClass('newclass')&&
$('#mytable.newclass')。长度<2);
})
Length不是一种方法,您缺少引号,并且选择器效率低下-1@minitech我正在编辑帖子,选择器是基于OP的。真的吗-1?我所说的选择器效率低下是多次选择,而不是缓存至少一次选择的结果。现在引号和缩进都好了,我将删除-1。这就是它的目的。是的。长度不是一种方法,你缺少引号,你的选择器效率低下-1@minitech我正在编辑帖子,选择器是基于OP的。真的吗-1?我所说的选择器效率低下是多次选择,而不是缓存至少一次选择的结果。现在引号和缩进都好了,我将删除-1。这就是它的目的。Yeesh.use.length
相反,它与.size()
的作用相同。。。根据jQuery文档,首选.length属性,因为它没有函数调用的开销。
最好使用.length
而不是.size()
,因为.size()
具有“函数调用的开销”。根据文档,使用.length
,它的作用与.size()
相同。。。根据jQuery文档,首选.length属性,因为它没有函数调用的开销。
根据文档,可能最好使用.length
而不是.size()
作为.size()
具有“函数调用的开销”。
$('#mytable td').click(function() {
var $this = $(this);
// toggle 'on' if
// 1. it doesn't already have the class and
// 2. and there are less than two set to .newclass
// toggle 'off' if
// 1. it already has the class and
// 2. and there are less than two set to .newclass
$this.toggleClass('newclass',
!$this.hasClass('newclass') &&
$('#mytable .newclass').length < 2);
})