我需要替换一组参数。当我这样做时,第二个参数(dir2)+++的Link1没有被替换(因为在Link1中,两个dir2都有相同的值)+++。我该如何解决这个问题?
var myArray = ["data/", "downloads/", "images/", "leftover/", "new2me/", "news/"];
$.each(myArray, function(key, value) {
$('.submenu').each(function() {
$(this).attr('href', ($(this).attr('href').replace("http://www.nu.nl/", "http://www.telegraaf.nl/").replace(value, 'latestdata/')));
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<li><a class="submenu" href="test.php?url1=http://www.nu.nl/&dir1=images/&url2=http://www.nu.nl/&dir2=images/" target="showme">Link1</a></li>
<li><a class="submenu" href="test.php?url1=http://www.nu.nl/&dir1=downloads/&url2=http://www.nu.nl/&dir2=new2me/" target="showme">Link2</a></li>
<li><a class="submenu" href="test.php?url1=http://www.nu.nl/&dir1=data/&url2=http://www.nu.nl/&dir2=leftover/" target="showme">Link3</a></li>
&#13;
编辑:见上文...... +++
之间答案 0 :(得分:0)
$(document).ready(function(){
var myArray = ["data/", "downloads/", "images/", "leftover/", "new2me/", "news/"];
//alert(myArray[0]);
for(i=0;i <myArray.length;i++){
$.each(myArray, function(key, value) {
$('.submenu').each(function() {
$(this).attr('href', ($(this).attr('href').replace("http://www.nu.nl/", "http://www.telegraaf.nl/").replace(value, 'latestdata/')));
});
});
}
});