2011-10-19 57 views
0

我试图做一个脚本,当点击该按钮时,将一个左右边框添加到3个按钮的一行中的按钮,否则保持不带边框。我到目前为止的代码是:从jquery设置样式属性

$("#popular").click(function(){ 
    clearBorders(); 
    //make borders here (this works) 
}); 

$("#suggestions").click(function(){  
    clearBorders(); 
    //make borders here (this works) 
}); 

$("recent").click(function(){  
    clearBorders(); 
    //make borders here (this works) 
}); 

function clearBorders(){ 
    $('popular').css("border", "solid"); 
    $('suggestions').css("border", "none"); 
    $('recent').css("border", "none"); 
}  
}); 

我能够创建边界罚款,但由于某些原因,clearborders方法是行不通的。我知道这个函数正在被调用,因为如果我在它的开始处发出警报,它就会显示出来。为什么这个功能不起作用?

回答

6

你的选择缺少龙头ID(#)或类(。)标志符号在你clearBorders()函数

+0

无论是id选择#或类选择。 – jcvandan

+0

@dormisher true。以#出现在其他选择器的基础上。我会修改答案 –

+0

好点,没有注意到,这是最有可能的# – jcvandan

1

我做了这个测试,你需要做$(“文件”)。就绪(函数(){});包装。我已经调整过使用类,所以你可能会或可能不会使用ID。至少下面的测试用例在我处理document.ready之前不适用于我。

<script type="text/javascript"> 
$("document").ready(function(){ 
    $(".popular").click(function(){ 
     clearBorders(); } 
    ); 

    $(".suggestions").click(function(){   
     clearBorders(); 
     // make borders here (this works) 
    }); 

    $(".recent").click(function(){  
    clearBorders(); 
    //make borders here (this works) 
    }); } 
); 
    function clearBorders(){ 
     $('.popular').css("border", "1px solid red"); 
     $('.suggestions').css("border", "1px solid red"); 
     $('.recent').css("border", "1px solid red"); 
    }; 
    </script> 

丰富