如何使用jquery更改文件的扩展名

时间:2016-06-10 11:56:47

标签: jquery

我的链接列表很少,扩展名为.pdf,我希望当用户点击该链接时,链接的整个文本都存储在变量中。
从那个变量我想重新.pdf到.jpg 我试过了

$(".links").click(function() {

  var clickedId= $(this).text();
  document.getElementById("thumb_url").value = "educroc.com/thumbs/"+clickedId+'.jpg';  

例如: 我有清单
<a href="#"> 1.pdf</a>

当用户点击clickedId=1.pdf时 我的结果是:1.pdf.jpg
要求的结果是:1.jpg

2 个答案:

答案 0 :(得分:1)

尝试:

$(".links").click(function() {

  var clickedId= $(this).text();
  clickedId = clickedId.replace(/\.[^/.]+$/, ""); // it will remove any extension from name
  document.getElementById("thumb_url").value = "educroc.com/thumbs/"+clickedId+'.jpg';  

});

答案 1 :(得分:0)

您不想更改文件的扩展名,您想要将链接中的字符串从“.pdf”更改为“.jpg”。

您可以使用jQuery获取文件的名称并替换它,如下所示:

$(".links").click(function() {
    var clickedId= $(this).text();
    var url = $('#thumb_url').html(); // bla/bla/bla.pdf
    var split = url.split('.');
    split[split.length - 1] = 'jpg'; // Last position of split is the extension
    var newUrl = split.join('.');
    $('#thumb_url').html(newUrl);

```

虽然您可能想要更改href属性而不是;)

的内容