简体   繁体   English

jQuery隐藏或显示元素,取决于其他元素的类

[英]jQuery hide or show element depending on other element's class

I would like to hide/show an element depending on the class of a specific element in the page (the class changes whether the element is selected or not). 我想根据页面中特定元素的类来隐藏/显示一个元素(该类会更改是否选择该元素)。 How could I do that with jQuery? 我该如何使用jQuery? I thought about using the click() or change() function and then check the element's class to hide() or show() the other element. 我考虑过使用click()或change()函数,然后检查该元素的类来隐藏()或show()其他元素。

I try with the following code but it does not work: 我尝试使用以下代码,但它不起作用:

 <script> jQuery(document).ready(function() { disableSectorAnalysis(); }); function disableSectorAnalysis(){ jQuery('#ygvtableel66').click(function(){ if (jQuery('#ygvtableel66').attr("class") == "ygtvtable ygtvdepth2 ygtv-highlight0") { jQuery('select[id*="SectorGliederung"]').hide(); } else jQuery('select[id*="SectorGliederung"]').show(); }); } </script> 

The html code is the following. html代码如下。 A 3-level tree is generated with Primefaces 2.2.1 library and I would like to hide/show the "Sectorgliederung element" when a specific node is unchecked/checked. Primefaces 2.2.1库生成了一个三层树,当未选中/选中特定节点时,我想隐藏/显示“ Sectorgliederung元素”。 But in Primefaces 2.2.1 there does not seem to be a method to trigger an event on a specific node selection, which is why I am trying to access the value of each node through jQuery. 但是在Primefaces 2.2.1中,似乎没有一种方法可以在特定的节点选择上触发事件,这就是为什么我试图通过jQuery访问每个节点的值的原因。

 <div class="punktlinie958"></div> <h2>Fondsmodulauswahl</h2> <table class="Formular" width="944px"> <tr> <th>Fondsmodule<br/>inkl. Zusatzmodule</th> <td> <p:tree value="#{treeFonds.root}" var="node3" selectionMode="checkbox" selection="#{treeFonds.selectedNodes}" propagateSelectionDown="true" propagateSelectionUp="true" expandAnim="FADE_IN" collapseAnim="FADE_OUT"> <p:treeNode> <h:outputText value="#{node3}"/> </p:treeNode> <p:treeNode type="nodesFondsAnalyseKlein" > <h:outputText value="#{node3}"/> </p:treeNode> <p:treeNode type="nodesPerformanceNetto"> <h:outputText value="#{node3}"/> </p:treeNode> <p:treeNode type="nodesPerformanceBrutto"> <h:outputText value="#{node3}"/> </p:treeNode> <p:treeNode type="nodesFondsAnalyseMittel"> <h:outputText value="#{node3}"/> </p:treeNode> <p:treeNode type="nodesFondsAnalyseGroß"> <h:outputText value="#{node3}"/> </p:treeNode> <p:treeNode type="nodesZusatzFonds"> <h:outputText value="#{node3}"/> </p:treeNode> </p:tree> </td> </tr> <tr> <th>Aktienanalyse<br/>nach Sektoren **</th> <td> <h:selectOneMenu id="SectorGliederung" value="#{fondsBean.fonds2mappe.sectorgliederung.id}" disabled="#{sitzungBean.prio1Disabled}" styleClass="gross" style="float:left"> <f:selectItems value="#{fondsBean.sectorgliederung}" /> </h:selectOneMenu> </td> </tr> 

Thank you for your help. 谢谢您的帮助。

You can use code like this: 您可以使用如下代码:

 $(document).ready(function() { hideElement(); }); function hideElement(){ $(document).on('change', '.changingelement', function(){ if($(this).val() === "on"){ $('#element').hide(); } else { $('#element').show(); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="radio" name="changingelement" value="on" class="changingelement">On <br/> <input type="radio" name="changingelement" value="off" class="changingelement">Off <div id="element">This is toggle div</div> 

I hope this sample code will help you out: 我希望此示例代码可以为您提供帮助:

 var divList = $(".parent div"); divList.each(function() { if ($(this).hasClass("active")) { $(this).addClass("color");//I have changed the color /* *You can hide/show or do any other function here. */ } }); 
 .color { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> <div>Inactive</div> <div>Inactive</div> <div class="active">This is the only active element.</div> <div>Inactive</div> <div>Inactive</div> <div>Inactive</div> </div> 

In the above example, active class has to be added when you are selecting the element. 在上面的示例中,选择元素时必须添加active类。 This class can come handy while performing other functions, that might be specific to the selected elements only. 在执行其他功能时,此类可能会派上用场,而这些功能可能仅特定于所选元素。 [Can be added on any of the events like change , click , blur , focus etc.] [可以添加到任何事件中,例如changeclickblurfocus等。]

Here is the reference fiddle for this solution. 是此解决方案的参考小提琴。

It's working for me. 它为我工作。 see this 看到这个

http://jsfiddle.net/DfPbJ/1/ http://jsfiddle.net/DfPbJ/1/

your HTML 您的HTML

<div class="statecontent state1">State1 Specific Page Content Goes here</div><br />
<div class="statecontent state1">State1 Specific Page2 Content Goes here</div><br />
<div class="statecontent state2">State2 Specific Page Content Goes here</div><br />
<div class="statecontent state3">State3 Specific Page Content Goes here</div><br />

<select id="myselector">
<option value="state1"></option><br />
<option value="state2"></option><br />
<option value="state3"></option><br />
</select>

your JQuery 您的JQuery

$('.statecontent').hide();
$('#myselector').change(function() {
$('.statecontent').hide();
$('.' + $(this).val()).show();    
});

your html like 你的HTML喜欢

<div id="changingelement" class="test"></div>

<div id="test"> test class details </div>

<span id="notTest"> Hello </span>

And your script will be, 您的脚本将是

if($('#changingelement').hasClass("notTest"))
{
  $('#test').hide();
} 
else($('#changingelement').hasClass("test"))
{
  $("#notTest").hide();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM