我正在使用此代码加载div的某些内容以及更改用户单击的选项卡的颜色。我无法弄清楚如何在用户单击其他内容后使背景颜色变回去。我该怎么做?
$(function() {
$('#tab-1').click(function() {
var tabcontent = "<?php echo preg_replace("/r?n/", "\n", addslashes($tabcontent[0]));?>";
document.getElementById('top-tabs-content').innerHTML = tabcontent;
$(this).css("background-color","#F7C703");
});
});
$(function() {
$('#tab-1').click(function() {
var tabcontent = "<?php echo preg_replace("/r?n/", "\n", addslashes($tabcontent[0]));?>";
document.getElementById('top-tabs-content').innerHTML = tabcontent;
$('.tabclass').css("background-color","#defaultcolour");
$(this).css("background-color","#F7C703");
});
});
向元素添加class
,然后在将颜色应用于clicked
element
之前,使用默认颜色重置所有elements
。
可以将单击处理程序绑定到document
对象并使用 .closest()
方法:
$(document).on('click', function(e) {
if ( !$(e.target).closest('#tab-1').length ) {
$('#tab-1').css("background-color", '');
}
});
http://jsfiddle.net/6g8g7/