javascript - 使用 javascript 获取锚标记 URL
问题描述
我正在尝试将锚标记的 url 存储在变量中,但我不断收到错误消息。
var buttonUrl = document.getElementById('test').href;
Uncaught TypeError: Cannot read property 'href' of null
解决方案
确保您的代码在窗口加载后正在运行:
window.onload = function() {
var buttonUrl = document.getElementById("test").href;
console.log(buttonUrl);
//This will log the href of the "test" element to the console as a debugger. You can now use buttonUrl as you want.
}
推荐阅读
- c# - 当我选中复选框并单击详细信息按钮后,我需要在弹出窗口中显示选定行的详细信息
- node.js - 如何在 cognito 中解决“客户端试图写入未经授权的属性”问题?
- javascript - 来自Angular 7组件的Meterial Design javascript方法
- c# - Botframework V4:在 Messenger 中重置 Bot
- .htaccess - 如何隐藏来自用户代理之一的重定向
- php - 可以在 Symfony 3.4 中禁用特定的弃用警告吗?
- design-patterns - GoF设计模式:不同种类的工厂
- python - 带有时区的时间戳,使用 django ORM 未知
- sql-server - 从 SSIS 检查 S3 可用性检查/Ping
- java - 如何在drools 7中创建可重用的KieBase