我知道有很多关于此的文章,但是我找不到我的特定问题的答案。

我想让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%">

关于javascript - HTML属性中的Javascript变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21643920/

10-12 00:20
查看更多