1

我想在一个 PHP 变量中设置窗口宽度(通过 JavaScript 获取)并用它来描述 div 容器的属性,但是下面的代码不起作用...

<?php
header("Content-type: text/css; charset: UTF-8"); // Parsing CSS zu PHP
echo "<script type='text/javascript'>
    if(typeof(window.innerWidth) !== 'number') {
        if(document.documentElement.clientWidth !== 0) {
            width  = document.documentElement.clientWidth;
            height = document.documentElement.clientHeight; 
        }
        else {
            width  = document.body.clientWidth;
            height = document.body.clientHeight;
        }
    }else {
        width  = window.innerWidth;
        height = window.innerHeight;
    }
    $cwidth = document.write(width);
    </script>";
?>
@charset "UTF-8";
#container
{
    width: <?php echo $cwidth . 'px'; ?>; 
}

问候

4

1 回答 1

0

您可以将其添加到 HTML 页面的底部,并在页面加载时由 JS 设置 #container 的宽度:

<script type='text/javascript'>
if(typeof(window.innerWidth) !== 'number') {
    if(document.documentElement.clientWidth !== 0) {
        width  = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight; 
    }
    else {
        width  = document.body.clientWidth;
        height = document.body.clientHeight;
    }
}else {
    width  = window.innerWidth;
    height = window.innerHeight;
}
document.getElementById('container').style.width = width;
</script>

顺便说一句:您在 JS 中编写了clinet而不是client

于 2013-09-10T10:19:21.003 回答