2017-06-08 62 views
-1

這很簡單,我已經做過,但是現在無法使它工作。用2個字替換href的一部分jquery

我需要更改以下HREF

var href="https://www.facebook.com/sharer/sharer.php?u=http://myurl.com/&description='tets'&picture=http://myurl.com/img/name-1654-45654.jpg" 

$('.share, .share-2').prop('href', function() { 
    $(this).replace(/(picture=).*?(&)/,'$1' + imgNew + '$2'); 
}); 
+0

這也將取代 「圖片=」 – AthMav

+0

我收到此錯誤$(...)代替的是不是一個功能。 –

+0

目前還不清楚預期的輸出結果應該是什麼或「imgNew」是什麼,但是URL對象可能是一種可行的方法。我在下面回答。 – styfle

回答

2

replace功能是string的方法,所以你不能從$(this)調用replace,因爲它是一個jQuery對象,而不是字符串。

如果您需要更改href屬性,請使用this.href = ...

編輯:因爲您正在使用jQuery.prop方法,您應該使用它作爲文檔建議。

$(".some-element").prop('some-prop', function(index, old_value){ 
    // do something 
    return new_value; 
}); 

片段更新

var new_img = "http://my.domain.com/img/my_new_image.jpg"; 
 
var regex_img = /\bpicture=[^&]*/ 
 

 
$('.share, .share-2').prop('href', function (index, old_href) { 
 
    var new_href = old_href.replace(regex_img, 'picture=' + new_img); 
 
    console.log(new_href); 
 
    return new_href; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<a href="https://www.facebook.com/sharer/sharer.php?u=http://myurl.com/&description='tets'&picture=http://myurl.com/img/name-1654-45654.jpg" class="share">Test</a> 
 
<br> 
 
<a href="https://www.facebook.com/sharer/sharer.php?u=http://myurl.com/&picture=http://myurl.com/img/name-1654-45654.jpg&description='tets'" class="share-2">Test-2</a>

+0

作品完美,謝謝 –

+0

這不能解決* actual *問題,即如何正確使用'$ .prop(屬性,函數)'。我建議相應地修復答案。看到我對這個問題的評論。 –

+0

其實他不需要使用'$ .prop'來做他所需要的。這就是爲什麼我建議使用'this.href'。他的錯誤是試圖使用'$(this).replace'。 你不覺得實際的問題是改變href中的'picture'參數嗎? 我不認爲它是關於使用'$ .prop'函數。 – fernandosavio

-1
var href="https://www.facebook.com/sharer/sharer.php? 
u=http://myurl.com/&description='tets'&picture=http://myurl.com/img/name- 
1654-45654.jpg" 
href.split('/') 
["https:", "", "www.facebook.com", "sharer", "sharer.php?u=http:", "", 
"myurl.com", "&description='tets'&picture=http:", "", "myurl.com", "img", 
"name-1654-45654.jpg"] 
    href.split('/').length 
12 
href.split('/')[11] 
"name-1654-45654.jpg" 
0

圖像的名字你應該改變你的代碼爲以下之一。

href="https://www.facebook.com/sharer/sharer.php?u=http://myurl.com/&description='tets'&picture=http://myurl.com/img/name-1654-45654.jpg" 

    $('.share, .share-2').prop('href', function() { 
     $(this).replace(/\/(?=[^\/]*$)/, '/newpicturename')); 
     }); 

最後一個斜線和後面的單詞將被替換爲新的圖片名稱。

一個例子

var str = "http://one/two/three/four"; 
 
console.log(str.replace(/\/(?=[^\/]*$)/, '/newpicturename'));

2

由於href字符串是一個URL,你可以採取URL對象的優勢。

var imgNew = 'http://example.com/img.png'; 
 
var href = "https://www.facebook.com/sharer/sharer.php?u=http://myurl.com/&description='tets'&picture=http://myurl.com/img/name-1654-45654.jpg"; 
 
var url = new URL(href); 
 
url.searchParams.set('picture', imgNew); 
 
console.log(url.href);

請注意,並非所有的瀏覽器都supported在這個時候,所以你可以使用一個polyfill

+0

據我所知,這個問題不是關於如何替換URL,而是關於如何使用jQuery的'$ (...)。prop(屬性,函數)'。 –

+0

如果沒有完整的代碼,很難知道哪個部分出現故障。 – styfle

+0

將[瀏覽器兼容性](https://developer.mozilla.org/en/docs/Web/API/URL#Browser_compatibility)添加到答案將會很好。 – fernandosavio