[英]Href attribute in JavaScript showing only hash
When I print out an object of the HTML a
tag: I see 当我打印出HTML的对象a
标签:我见
href: "http://www.example.com/page.html#abc.1.2"
hash: "#abc.1.2"
When I do 当我做
$(document.body).on('click',"a",function(event){
console.log( $(this).attr("href") );
});
I only get "#abc.1.2"
, which is the value of the hash, not the whole URL. 我只得到"#abc.1.2"
,它是哈希值,而不是整个URL。 Why is that, and how can I get the whole URL? 为什么会这样,如何获得整个URL?
Because you are getting the value of the attribute and not the property. 因为您获取的是属性的值而不是属性。
Use .prop
instead of .attr
. 使用.prop
而不是.attr
。
Not sure what you're doing but this returns the HREF along with the host name and protocol 不确定您在做什么,但这会返回HREF以及主机名和协议
<a href="#abc.1.2">Link</a>
$(document).on('click',"a",function(event){
console.log( this.href );
return false;
});
You don't need to wrap this
with jQuery to return a common property. 您不需要使用jQuery来包装this
即可返回一个公共属性。 Use instead this.href
. 改用this.href
。
$(document).on('click',"a",function(e){
e.preventDefault();
console.log(this.href)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.