简体   繁体   English

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

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

Let's say I have the following DIVs: 假设我有以下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>

How can I hide all the DIVs which id start with the word IDphoto except for the DIV which ID has the value of IDphotoFromURL? 我如何隐藏ID开头为IDphoto的所有DIV,但ID值为IDphotoFromURL的DIV除外? For instance if IDphotoFromURL= IDphoto3 then all DIVs will be hidden except the one with id = IDphoto3 ... 例如,如果IDphotoFromURL = IDphoto3,则所有DIV将被隐藏,除了ID = IDphoto3的那个...

You can use the attribute starts with selector, with .not() 您可以使用选择器开头的 .not() 属性

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

You could use filter() for this: 您可以为此使用filter()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能关闭除打开的那个之外的所有 div? - How can I close all divs except the one which is open? 如何在angularjs中隐藏除第一类之外的同一类的所有div - How to hide all the divs of same class except first class in angularjs 隐藏除第一个孩子以外的所有子div。 当我从第一格中选择一个广播选项时,显示第二个 - Hide all children divs except first one. When I select a radio option from first div, show the second one 如何使用.not()隐藏除单击的div外的所有div - How to use .not() to hide all divs except the one which was clicked 如何用jQuery隐藏除一个以外的所有div? (未定义的函数错误) - How to hide all divs except for one with jQuery? (undefined function error) 如何隐藏除一个以外的所有div并否定先前的js? - How to hide all divs except for one and negating the previous js? 如何隐藏除被点击的所有其他 div? - How to hide all other div except one that is clicked? 如何在可可网络视图中隐藏除一个div以外的所有内容? - How to hide all except for one div in a cocoa web view? 我将如何选择所有具有相同名称的div类,但使用jQuery / javascript的当前div除外? - How would I select all div classes with the same name except the one in the current div with jquery/javascript? 隐藏除最后2个外的所有Divs。Divs的数量可以增加 - Hide all Divs except last 2. Number of Divs can increase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM