javascript - 如何将解析后的URL值从javascript发送到html中img标签的src?

标签 javascript html

我正在创建一个名为 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/

相关文章:

javascript - 如何允许用户编辑 HTML 标签内的格式化文本?

javascript - 复选框被选中时的 if 语句?

javascript - ng-options 不适用于基本情况

javascript - jQuery - 使用 replaceWith 时显示加载消息

javascript - 将 localStorage 值加一

javascript - jquery 验证错误 预期的标识符、字符串或数字

javascript - Promise 的 then 处理程序中的同步代码

html - 如何用更少的空间 HTML/CSS 创建新行

php - javascript事件不处理脚本

php - 分页后显示当前表头 css html