Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取用javascript编写的href属性_Javascript_Jquery_Dhtml - Fatal编程技术网

获取用javascript编写的href属性

获取用javascript编写的href属性,javascript,jquery,dhtml,Javascript,Jquery,Dhtml,我希望获得html中显示的href属性,而不是完全限定的url <a href="foo.html">foo</a> $('a[href]').each(function(){ alert(this.href); }); $('a[href]')。每个(函数(){ 警报(this.href); }); 获取以http:///开头的绝对url。我希望它改为“foo.html”。尝试.attr('href')获取href值。尝试.attr('href')获取hr

我希望获得html中显示的href属性,而不是完全限定的url

<a href="foo.html">foo</a>

$('a[href]').each(function(){
   alert(this.href);
});

$('a[href]')。每个(函数(){
警报(this.href);
});

获取以http:///开头的绝对url。我希望它改为“foo.html”。

尝试
.attr('href')
获取href值。

尝试
.attr('href')
获取href值。

您需要改变循环元素的方式,以及使用
的方式:

$('a').each(function(){
   alert( $( this ).attr( 'href' ) );
});

您需要改变元素循环的方式,以及使用
的方式:

$('a').each(function(){
   alert( $( this ).attr( 'href' ) );
});

不错!非常感谢。我需要更多地阅读JQuery。每次我思考,我都明白了——它又欺骗了我!美好的非常感谢。我需要更多地阅读JQuery。每次我思考,我都明白了——它又欺骗了我!此.attr()生成一个错误。你是在建议,放下这个,然后使用裸.attr?我的意思是
$(this.attr('href')
,因为
这个
是对domeElement的引用,它没有attr方法,但是当用jQuery包装时,它有。这个.attr()会生成一个错误。你是不是在建议,放下这个,用裸体的.attr?By
.attr
我的意思是
$(this.attr('href')
,因为
这个
是对domeElement的引用,它没有attr方法,但是当用jQuery包装时它有。