如何隐藏所有以相同单词开头的DIV,除了一个具有特定值的DIV

时间:2015-02-03 13:17:00

标签: javascript jquery asp.net

我们说我有以下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 ...

2 个答案:

答案 0 :(得分:1)

您可以attribute starts with

使用.not()选择器
var IDphotoFromURL = getUrlParameter('IDphoto');
$('div[id^="IDphoto"]:not(#' + IDphotoFromURL + ')').hide();

答案 1 :(得分:1)

您可以使用filter()

$('[id^="IDphoto"]').filter(function() {
    return this.id != "IDphoto" + IDphotoFromURL;
}).hide();