[英]Get anchor tag URL with javascript
我正在尝试将锚标记的 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.
}
有时您需要像零一样引用元素的索引。 试试这个
var buttonUrl = document.getElementById('test')[0].href
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.