我正在创建一个名为 test.html 的 html 文件。 我使用 JavaScript 解析了 URL 来获取解析后的参数。
URL 看起来像这样(示例 URL)
http://path.to.a.webserver/test.html?p1=welcome&p2=home
解析值后,我想以以下格式将解析后的值发送到 HTML img 标记。当我点击上面的 URL 时,值 p1 和 p2 被解析,并且 test.html 文件中的 img 标记被执行。:
<img src="http://example2.com/tiger.gif?p1={p1_parsed_value}&p2={p2_parsed_value}"
我尝试过使用 document.getElementById。我不确定这是否正确。如何仅将那些已解析的 URL 值传递到 img 标记的 src 中?
最佳答案
var img = document.querySelector("img");//select node
img.setAttribute("src",url);//set src attribute to required url
关于javascript - 如何将解析后的URL值从javascript发送到html中img标签的src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45257395/