HTML 属性中的 Javascript 变量 [英] Javascript variables in HTML attributes
本文介绍了HTML 属性中的 Javascript 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道有很多关于这个的帖子,但我找不到我的具体问题的答案.
I know there are a lot of posts about this, but I can't find an answer to my specific problem.
我想让一个 JS 变量成为一个 HTML 属性的值:
I would like to make a JS variable the value of an HTML attribute:
<script> var screenWidth = screen.width </script>
<img src="images/title.png" width="VARIABLE HERE" style="margin-top: 3%"`
VARIABLE HERE"
是我希望 screenWidth
变量去的地方.这样做的最佳方式是什么?
"VARIABLE HERE"
is where I would want the screenWidth
variable to go. What is the best of going about this?
推荐答案
这应该有效:
<script>var screenWidth = screen.width;</script>
...
<img src="images/title.png" onload="this.width=screenWidth;" style="margin-top: 3%">
这篇关于HTML 属性中的 Javascript 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文