html - 基于 GET 参数的动态 href
问题描述
我想在 HTML 中创建一个链接,其href
更改基于 GET 参数(示例链接)
当href
我尝试这个
<a href="?url">text</a>
但它不起作用
对不起我的英语不好
解决方案
I understand that you're trying to fill out the "href"-attribute of an anchorlink, by dynamically getting an URL from the GET parameter.
This can be done with the help of JavaScript. I found an example in another Stack Overflow question and made it fit your use case. In that same thread you'll find some good examples of other ways to do it as well.
Try out the following snippet:
<a id="myLink" href="">Text</a>
<script>
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('link');
const myLink = document.getElementById('myLink');
if(myParam) {
myLink.href = myParam; /* The URL given as a parameter */
} else {
myLink.href = '#0'; /* If no parameter, the link leads nowhere */
}
</script>
推荐阅读
- matlab - 在方程组的 MATLAB 中查找正态模式
- sql - SQL,随机排序和限制不起作用?
- java - 如何在同一个项目的intellij中使用Java和python
- javascript - 如何根据文档日期在财政年度中设置日期选择器范围?我使用 JQUERY 来执行此操作
- command-line - cli --dry-run 的输出应该是什么?
- java - Lombok @Getter 和 @Setter 无法获取或设置对象的值
- emacs - Emacs 默认保存目录标准
- android - 多个dex文件定义Landroid/support/v4/os/ResultReceiver;
- java - 解决 HashMap 哈希冲突的技术是否可配置
- excel - Excel VBA Application.Find() 方法