我知道有很多关于此的帖子,但我找不到我的具体问题的答案。
我想让一个 JS 变量成为一个 HTML 属性的值:
<script> var screenWidth = screen.width </script>
<img src="images/title.png" width="VARIABLE HERE" style="margin-top: 3%"`
"VARIABLE HERE"
是我希望 screenWidth
变量所在的位置。最好的做法是什么?
最佳答案
这应该有效:
<script>var screenWidth = screen.width;</script>
...
<img src="images/title.png" onload="this.width=screenWidth;" style="margin-top: 3%">
关于HTML 属性中的 Javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643920/