简体   繁体   English

获取不带“ .jpg”部分的文件名

[英]Geting a file name without the “.jpg” part

Ok I am using slimbox2 and want the user to be able to save a Hi-Res version of the image. 好的,我正在使用slimbox2,希望用户能够保存该图像的高分辨率版本。 Adding this to the .js file allows the EXACT same image to be saved with a link in the title: 将其添加到.js文件中,可以保存完全相同的图像,并在标题中带有链接:

    // AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED) 
    jQuery(function($) {
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, function(el) {
    return [el.href, el.title + '<br /><a href="' + el.href + '">
    Download this image</a>'];
    }, function(el) {
            return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
    });

However I want the user to be able to save a Hi-Res version with just "lg" added to the name. 但是,我希望用户能够通过在名称中添加“ lg”来保存高分辨率版本。

for instance instead of downloading "image1.jpg" which is displayed, it downloads "image1lg.jpg" 例如,下载的是“ image1lg.jpg”,而不是下载显示的“ image1.jpg”

The code above uses el.href to load the image into the link but if I use + 'lg' it adds that BEHIND the .jpg, so I get "image1.jpglg" which won't work. 上面的代码使用el.href将图像加载到链接中,但是如果我使用+'lg',则会在BEHIND .jpg后面添加内容,因此我得到了“ image1.jpglg”,它将无法正常工作。

Is there a way to grave the el.href and then subtract just the ".jpg" from the name, then add the "lg.jpg" to the file name? 有没有一种方法可以对el.href进行雕刻,然后从名称中减去“ .jpg”,然后将“ lg.jpg”添加到文件名中?

Hope that makes sense, thanks for the time. 希望有道理,谢谢您的时间。

This answer is not an be-all end-all answer but it does solve your specific question. 这个答案不是万能的,但确实可以解决您的特定问题。

This script will essentially fail if the filename has more than one period 如果文件名包含多个句点,则该脚本实际上将失败

Yes there is a Javascript split() function: 是的,有一个Javascript split()函数:

// The file
var string = 'filename.jpg'

// Split on periods
var parts = string.split('.');

// parts[0] = filename
// parts[1] = jpg

// Bring the string back together and add an 'lg' where needed
var new_string = parts[0]+'lg.'+parts[1];

如果lg始终是扩展名前文件名的最后一部分,请尝试以下操作:

return [el.href, el.title + '<br /><a href="' + el.href.replace(/\.jpg$/i, 'lg.jpg') + '">Download this image</a>'];

I would use javascript's replace function. 我会使用javascript的替换功能。 You can match a string or regex expression and replace it with whatever you want, in this case matching '.jpg' and replacing it with 'lg.jpg'. 您可以匹配字符串或正则表达式,然后将其替换为所需的内容,在这种情况下,将匹配“ .jpg”并将其替换为“ lg.jpg”。

jQuery(function($) {
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, function(el) {
        return [el.href, el.title + '<br /><a href="' + el.href.replace('.jpg', 'lg.jpg') + '"> Download this image</a>'];
    }, function(el) {
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
});

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

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