使用 jQuery 删除部分文件名

发布于 2024-10-22 03:22:07 字数 404 浏览 3 评论 0原文

我想删除这样的文件名: IMG_6903.JPG&width=504

到:

IMG_6903.JPG

...

我的脚本看起来像这样:

  $(function(){
    $('#exposure img').each(function(){
        var $imgSrc = this.src.split('&');
        $(this).wrap('<a rel="lightBox" />')
               .parent().attr("href", ""+$imgSrc);
      });
  });    

但它不起作用...我该怎么做?

I want to strip a filename like this:
IMG_6903.JPG&width=504

to:

IMG_6903.JPG

...

My script looks like this:

  $(function(){
    $('#exposure img').each(function(){
        var $imgSrc = this.src.split('&');
        $(this).wrap('<a rel="lightBox" />')
               .parent().attr("href", ""+$imgSrc);
      });
  });    

But it doesn't work... How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱情眠于流年 2024-10-29 03:22:07
var imgSrc = this.src.substring(0,this.src.indexOf('&'));
var imgSrc = this.src.substring(0,this.src.indexOf('&'));
梦一生花开无言 2024-10-29 03:22:07
var andIndex = tmpString.indexOf('&');
tmpString = tmpString.substr(0, andIndex);
var andIndex = tmpString.indexOf('&');
tmpString = tmpString.substr(0, andIndex);
_畞蕅 2024-10-29 03:22:07

使用split

var str = 'IMG_6903.JPG&width=504';
var array = str.split('&');
array[0];

数组[0]将是IMG_6903.JPG

use split

var str = 'IMG_6903.JPG&width=504';
var array = str.split('&');
array[0];

array[0] will be IMG_6903.JPG

冰之心 2024-10-29 03:22:07

您分割字符串,但尝试使用数组而不是第一个元素。试试这个

 $(function(){
     $('#exposure img').each(function(){
         var $imgSrc = this.src.split('&');
         $(this).wrap('<a rel="lightBox" />')
             .parent().attr("href", "" + $imgSrc[0]);
     });
 });

希望这有帮助。

You split the string but try using the array instead of the first element. Try this

 $(function(){
     $('#exposure img').each(function(){
         var $imgSrc = this.src.split('&');
         $(this).wrap('<a rel="lightBox" />')
             .parent().attr("href", "" + $imgSrc[0]);
     });
 });

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文