簡體   English   中英

我如何隱藏所有以相同單詞開頭的DIV,只有一個具有特定值的DIV

[英]How can I hide all DIVs that start with the same word except for one DIV that has a specific value

假設我有以下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>

我如何隱藏ID開頭為IDphoto的所有DIV,但ID值為IDphotoFromURL的DIV除外? 例如,如果IDphotoFromURL = IDphoto3,則所有DIV將被隱藏,除了ID = IDphoto3的那個...

您可以使用選擇器開頭的 .not() 屬性

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

您可以為此使用filter()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM