1
votes

Comment puis-je changer le domaine img src?

S'il y a du code HTML comme ci-dessous

<img src="https://example2.com/img_01.png">
<img src="https://example2.com/img_02.png">
<img src="https://example2.com/img_03.png">
<img src="https://example2.com/img_04.png">

Je voudrais changer ceux-ci comme:

<img src="https://example.com/img_01.png">
<img src="https://example.com/img_02.png">
<img src="https://example.com/img_03.png">
<img src="https://example.com/img_04.png">

Comment puis-je faire cela ?


0 commentaires

5 Réponses :


0
votes

Vous pouvez utiliser .attr ()

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://example.com/img_01.png">
<img src="https://example.com/img_02.png">
<img src="https://example.com/img_03.png">
<img src="https://example.com/img_04.png">
$('img').attr('src',"https://example2.com/img_01.png");
console.log(Array.from(document.querySelectorAll('img')))


2 commentaires

Je ne peux pas modifier manuellement, je veux dire que je dois changer automatiquement example.com en example2.com


@Te Ggh ce que vous entendez par là. Voir le résultat de l'extrait de code le code modifié



0
votes

Vous pouvez le faire avec jQuery:

$("img").each(img => img.src.replace("example.com", "example2.com"));

Code JavaScript:

<img src="https://example.com/img_01.png">
<img src="https://example.com/img_02.png">
<img src="https://example.com/img_03.png">
<img src="https://example.com/img_04.png">


0 commentaires

2
votes

Vous pouvez utiliser replace pour changer url à l'intérieur de la boucle afin d'obtenir une nouvelle url comme ci-dessous.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://example.com/img_01.png" />
<img src="https://example.com/img_02.png" />
<img src="https://example.com/img_03.png" />
<img src="https://example.com/img_04.png" />
$('img').each(function() {
  var newSrc = $(this).attr('src');
  newSrc = newSrc.replace('example', 'example2')
  $(this).attr('src', newSrc);

});


0 commentaires

0
votes

Vous pouvez essayer comme ceci

​​

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<img src="https://example.com/img_01.png">
<img src="https://example.com/img_02.png">
<img src="https://example.com/img_03.png">
<img src="https://example.com/img_04.png">
var newDOmain = "https://example2.com/";

$('img').each(function(){
  var imgName = $(this).attr('src').split('/').pop();
  $(this).attr('src', newDOmain + imgName);
  $(this).attr('alt', newDOmain + imgName);
})


0 commentaires

0
votes

si un jour votre URL change en " https://example.net/img_01.png "alors vous devez modifier le code si vous utilisez des remplacements de chaîne directs. Donc, pour rendre votre code plus efficace, utilisez la manière regex de faire de même.

var domainRegex = /\/{2,}((?:[^\.]+\.)*\w+)\//;
$("img").each(img => {
    img.src = img.src.replace(img.src.match(domainRegex)[1], "example2.com");
});

Cette expression régulière peut remplacer n'importe quel nom de domaine par celui que vous avez choisi.


0 commentaires