为什么这段 JavaScript 代码不起作用?

发布于 2024-10-09 17:52:55 字数 1243 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-10-16 17:52:55

您有几个语法错误,以下是错误列表:

  • 您需要调用 document.getElementById()
  • 不需要 +\,这是一个格式不正确的文字。 .但这里无论如何都不需要
  • widthstyle 的一个属性,只需像我下面那样设置它,确保包含 "px"< /code> 放在最后。

总的来说,它应该只是:

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>

You have several syntax errors, here's a list of what's wrong:

  • You need to call document.getElementById()
  • +\ isn't needed, that's an improperly formatted literal...but isn't not needed here anyway
  • The width is a property on style, just set it like I have below, make sure to include "px" on the end.

Overall, it should just be:

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>

撩起发的微风 2024-10-16 17:52:55

你能试试这个吗?

var contentObj = document.getElementById("content");
var widthOfContent = screen.availWidth * 0.6;
var cssContObjStr = "width:" + (screen.availWidth * 0.6) + "%;";
contentObj.style.cssText = cssContObjStr;

谢谢

Can you please try this?

var contentObj = document.getElementById("content");
var widthOfContent = screen.availWidth * 0.6;
var cssContObjStr = "width:" + (screen.availWidth * 0.6) + "%;";
contentObj.style.cssText = cssContObjStr;

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文