var contentObj = getElementById("content");
var widthOfContent = screen.availWidth * 0.6;
var cssContObjStr = "width:" + (screen.availWidth * 0.6) + ";";
contentObj.style = cssContObjStr;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing</title>
</head>
<body>
<div id="content"></div>
jdhkfahsd jksahd fklhasdk fhaskdhf kalsdhf klashdfk hasdkf haskd hfasdkjfhkjasdh fkjasdkfh asjdkhf khdskf jhaskdjh fkjasdhf kjsahd fkasbdkfbsadvn iweb fnilewalnfiwa ebnkjsdbv jdbalisd df sads da s fsd afds fasdf sadf asdf adsf asdf sdf
<script type="text/javascript" src="javas.js"></script>
</body>
</html>
我已将javascript文件保存为javas.js
在我的电脑上。但是,我正在尝试将宽度设置为屏幕宽度的60%(以像素为单位)。这不起作用。
我该如何管理?请不要说“将其设置为width:60%
”。
答案 0 :(得分:6)
你有几个语法错误,这里有一个错误列表:
document.getElementById()
+\
,这是一个格式不正确的文字...但不管怎么说都不需要width
是style
上的属性,只需将其设置为如下所示,请务必在最后添加"px"
。总的来说,它应该是:
var contentObj = document.getElementById("content");
var widthOfContent = screen.availWidth * 0.6;
contentObj.style.width = widthOfContent + "px";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing</title>
</head>
<body>
<div id="content"></div>
jdhkfahsd jksahd fklhasdk fhaskdhf kalsdhf klashdfk hasdkf haskd hfasdkjfhkjasdh fkjasdkfh asjdkhf khdskf jhaskdjh fkjasdhf kjsahd fkasbdkfbsadvn iweb fnilewalnfiwa ebnkjsdbv jdbalisd df sads da s fsd afds fasdf sadf asdf adsf asdf sdf
<script type="text/javascript" src="javas.js"></script>
</body>
</html>
答案 1 :(得分:0)
你能试试吗?
var contentObj = document.getElementById("content");
var widthOfContent = screen.availWidth * 0.6;
var cssContObjStr = "width:" + (screen.availWidth * 0.6) + "%;";
contentObj.style.cssText = cssContObjStr;
由于