Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IE6中的jQuery中心Div切换_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript IE6中的jQuery中心Div切换

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

单击按钮以居中div,再次单击以左对齐。这适用于除IE6之外的所有浏览器。IE6不支持
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">