在HTML5中,可以通过JavaScript动态添加路径到href
和src
属性中。以下是一种常见的方法:
id
属性。<a id="myLink" href="">Link</a>
<img id="myImage" src="" alt="Image">
setAttribute
方法来动态设置路径。var link = document.getElementById("myLink");
var image = document.getElementById("myImage");
var linkPath = "http://example.com/link";
var imagePath = "http://example.com/image.jpg";
link.setAttribute("href", linkPath);
image.setAttribute("src", imagePath);
在上面的例子中,我们将href
属性设置为http://example.com/link
,将src
属性设置为http://example.com/image.jpg
。
请注意,linkPath
和imagePath
是你需要动态添加的路径,你可以根据实际需求进行修改。
这种方法适用于任何需要动态添加路径的HTML元素,无论是链接、图像还是其他资源。
领取专属 10元无门槛券
手把手带您无忧上云