为什么这段 JavaScript 代码不起作用?
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%
”。
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>
I've saved the javascript file as javas.js
on my pc. However, I'm trying to set the width as 60% of the screen width in pixels. This isn't working.
How can I manage this? And please, don't say "set it to width:60%
".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有几个语法错误,以下是错误列表:
document.getElementById()
+\
,这是一个格式不正确的文字。 .但这里无论如何都不需要width
是style
的一个属性,只需像我下面那样设置它,确保包含"px"< /code> 放在最后。
总的来说,它应该只是:
You have several syntax errors, here's a list of what's wrong:
document.getElementById()
+\
isn't needed, that's an improperly formatted literal...but isn't not needed here anywaywidth
is a property onstyle
, just set it like I have below, make sure to include"px"
on the end.Overall, it should just be:
你能试试这个吗?
谢谢
Can you please try this?
Thanks