如何从anchor href属性获取域名? [英] How to get the domain name from anchor href attribute?
本文介绍了如何从anchor href属性获取域名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网址,例如:
http://www.intereconomia.com/noticias-gaceta/politica/grinan-los-casos-corrupcion-pueden-influir-20120226
我只想要我的链接文本:
I want only in my link text:
http://www.intereconomia.com
但href转到:
http://www.intereconomia.com/noticias-gaceta/politica/grinan-los-casos-corrupcion-pueden-influir-20120226
什么正则表达式jQuery可以实现此功能?
What regex jquery can do this functionality?
推荐答案
除了使用正则表达式外,您还可以尝试使用这种简单而又干净的方法.
Instead of using a regex you can try this which is clean and simple.
$('a').each(function(){
$(this).text(this.protocol + "//" + (this.hostname || this.pathname));
});
注意:如果只想为一组锚设置它,则相应地更改选择器,但内部逻辑保持不变.
Note: If you want to set it only for set of anchors, then change the selector accordingly but the logic inside remains the same.
工作演示- http://jsfiddle.net/ShankarSangoli/8G7JM/3/
这篇关于如何从anchor href属性获取域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文