简体   繁体   English

如何从Jquery中的src URL属性中删除域名

[英]How to remove domain name from the src URL attribute in Jquery

I'm fetching img src attribute into a variable, while doing so I'm getting the complete url of the image, i want to remove domain name from the url 我正在将img src属性提取到变量中,同时这样做我正在获取图像的完整URL,我想从网址中删除域名

var imgurl = "http://nitseditor.dev/img/home/bg.jpg";

I want to have only img/home/bg.jpg . 我想只有img/home/bg.jpg How can I achieve it? 我怎样才能实现它?

You can use URL constructor. 您可以使用URL构造函数。

This is an experimental technology 这是一项实验技术

var url = new URL(imgurl);
console.log(url.pathname);

 var imgurl = "http://nitseditor.dev/img/home/bg.jpg"; var url = new URL(imgurl); console.log(url.pathname); 

Browser Support 浏览器支持

Get substring by getting the index of third / . 通过获得第三索引获取子/

 var imgurl = "http://nitseditor.dev/img/home/bg.jpg"; console.log( imgurl.substr(imgurl.indexOf('/', 7) + 1) ); 

url = url.replace(/^.*\/\/[^\/]+/, '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM