我们说我有以下DIV:
<div id="IDphoto1">photo1</div>
<div id="IDphoto2">photo2</div>
<div id="IDphoto3">photo3</div>
<div id="IDphoto4">photo4</div>
<div id="IDphoto5">photo5</div>
<scipt>
var IDphotoFromURL = getUrlParameter('IDphoto');
function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
</script>
如何隐藏所有以IDphoto开头的ID,除了ID为IDphotoFromURL的ID的DIV外?例如,如果IDphotoFromURL = IDphoto3,那么所有DIV都将被隐藏,除了id = IDphoto3 ...
答案 0 :(得分:1)
var IDphotoFromURL = getUrlParameter('IDphoto');
$('div[id^="IDphoto"]:not(#' + IDphotoFromURL + ')').hide();
答案 1 :(得分:1)
您可以使用filter()
:
$('[id^="IDphoto"]').filter(function() {
return this.id != "IDphoto" + IDphotoFromURL;
}).hide();