如何根据另一个部分的计算宽度设置一个部分的宽度?
尝试这样做,但这是错误的吗?下面是我尝试加载的样式、脚本和 html。
<style>
#bar {
background:black;
width: </style><script>var barWidth = getElementByID("nav").style.width; document.write (nav);</script><style>
height: 5px;
}
</style>
<section id="nav">
<a href="#">Link1</a>
<a href="#">Link2</a>
<a href="#">Link3</a>
<span id="loginButton"><a href="#">Login</a></span>
<section id="bar"></section>
</section>
Trying to do this, but is it wrong? Below is my style and script and html that I am trying to load.
<style>
#bar {
background:black;
width: </style><script>var barWidth = getElementByID("nav").style.width; document.write (nav);</script><style>
height: 5px;
}
</style>
<section id="nav">
<a href="#">Link1</a>
<a href="#">Link2</a>
<a href="#">Link3</a>
<span id="loginButton"><a href="#">Login</a></span>
<section id="bar"></section>
</section>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将
script
标记嵌套在style
标记内。以下将实现您想要的行为......You cannot nest a
script
tag within astyle
tag. The following will accomplish your desired behavior...嗯,是的,你不能那样做...
script
和style
不像 PHP 或 ASP 之类的切换。但是,您应该为此使用 js。如果将
#bar
设置为块级元素,默认情况下它将是#nav
的宽度:Umm yeah you cant do that...
script
andstyle
arent like switching in and out of PHP or ASP or something.However you shoudlnt need to use js for this. if you set
#bar
to a block level element it will be the width of#nav
by default:在您的示例中,它只是
width:100%
因为 #bar 是 #nav 的直接后代。但是,您应该使用,而不是使用
“如何动态设置CSS规则?”这个问题的答案可以在文章Totally Pwn CSS with Javascript中找到。不要使用此解决方案来解决您的问题。
In your example, it would be just
width:100%
as #bar is a direct descendant of #nav. However, instead of making use of a <section>-element you either should use <hr />, or better put a simple border-bottom for #nav.The answer to the question "how to set a css rule dynamically?" might be found in the article Totally Pwn CSS with Javascript. Do not use this solution for your problem.