Javascript IE6中的jQuery中心Div切换
单击按钮以居中div,再次单击以左对齐。这适用于除IE6之外的所有浏览器。IE6不支持Javascript IE6中的jQuery中心Div切换,javascript,jquery,html,css,Javascript,Jquery,Html,Css,单击按钮以居中div,再次单击以左对齐。这适用于除IE6之外的所有浏览器。IE6不支持margin:0自动如何解决此问题。div width是动态的,并不总是200px 检查jsfiddle 左对齐 $('#中间')。切换(函数(){ $('div').css('margin','0'); $(此).text('中心对齐') },函数(){ $('div').css('margin','0 auto'); $(this).text('左对齐') }); 供IE6使用 body{ text-ali
margin:0自动代码>如何解决此问题。div width是动态的,并不总是200px
检查jsfiddle
左对齐
$('#中间')。切换(函数(){
$('div').css('margin','0');
$(此).text('中心对齐')
},函数(){
$('div').css('margin','0 auto');
$(this).text('左对齐')
});
供IE6使用
body{
text-align:center;
}
您的jQuery代码如下所示
$('#center').toggle(function() {
$('div').css('margin', '0');
$('body').css('text-align', 'left');
$(this).text('Center Align')
}, function() {
$('div').css('margin', '0 auto');
$('body').css('text-align', 'center');
$(this).text('Left Align')
});
检查中的工作示例此解决方案适用于IE6中的我
$('#center').click(function () {
$('div').toggleClass('center');
});
类
.center { margin: 0px auto; }
在可能的情况下,将JS和CSS分开也是一种很好的做法
这是我的doctype
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@Pinkie以下是我在IE6测试中使用的确切代码:@Pinkie我没有使用怪癖模式。尝试使用我发布的doctype。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">