【发布时间】:2018-09-25 11:42:18
【问题描述】:
我想根据页面中特定元素的类隐藏/显示一个元素(无论元素是否被选中,类都会改变)。我怎么能用 jQuery 做到这一点?我考虑过使用 click() 或 change() 函数,然后检查元素的类以 hide() 或 show() 另一个元素。
我尝试使用以下代码,但它不起作用:
<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>
html 代码如下。使用 Primefaces 2.2.1 库生成一个 3 级树,我想在未选中/选中特定节点时隐藏/显示“Sectorgliederung 元素”。 但在 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>
感谢您的帮助。
【问题讨论】:
-
请创建一个合适的minimal reproducible example。
-
为什么在触发更改类的操作时不隐藏/显示该元素?
-
您能否详细说明您的问题。你在说什么其他类?可以在帖子中添加相关的html代码吗?
-
您不是按类选择元素,而是按 id。将
'#element'更改为'.element'将按类选择元素。 -
感谢您的快速回答,我更新了帖子。
标签: javascript jquery