gpt4 book ai didi

处理可折叠 div 和子 div 的 Javascript

转载 作者:行者123 更新时间:2023-11-30 13:30:49 26 4
gpt4 key购买 nike

我有一个用 js 实现的可折叠 div,其功能是显示/隐藏它的子 div 等。

因此,当您单击它时,它会切换 div 的消失。类似于 TreeView 的东西

我想做的是,我应该能够选择子 div 中的数据,但问题是。当我点击子 div 时,父 div 会消失,因为我猜 onclick 会被触发。

请建议我应该如何实现这一点。所以我的目标是切换包含子 div 的 div 显示,但是如果您在子 div 中选择某些内容或突出显示某些内容,则父 div 不应折叠等。

请看下面的代码。

                        <html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Data</title>
<style>
#container {
clear: both;
width: 400px;
margin: 0 0;
border: none;
}

#row {
clear: both;
width: 100%;
border: none;
}

#column {
border: double #aca899;
float: left;
clear: none;
width: 402px;
}

#columnHeader p {
float: left;
clear: none;
text-align: center;
width: 100%;
height: 20px;
padding-top: 2px;
font-size: 8.0pt;
font-family: Verdana;
font-weight: bold;
color: black;
text-decoration: underline;
border: 1px ridge #aca899;
}

#subSectionHeader {
float: left;
clear: none;
width: 50%;
font-size: 7.5pt;
font-family: Arial;
background-color: #DBA083;
border: 1px ridge #aca899;
border-collapse: collapse;
padding: 1px;
}


#label {
float: left;
clear: none;
width: 50%;
font-size: 7.5pt;
font-family: Arial;
background-color: #F2CF93;
border: 1px ridge #aca899;
padding: 1px 1px 1px 11px;
}


#Value {
float: left;
clear: none;
width: 50%;
font-size: 7.5pt;
font-family: Arial;
background-color: #DDDDDD;
border: 1px ridge #aca899;
padding: 1px;
}

</style>
<script type="text/javascript">
function switchMenu(cur, e) {
var obj = e.target || e.srcElement; //works on IE and FF
var childDiv = cur.id + 'Child';


if (obj.parentNode.id != childDiv) {
var el = document.getElementById(childDiv);
if (el.style.display != "none") {
el.style.display = 'none';
}
else {
el.style.display = '';
}
}
}

</script>
</head>

<body>
<div id="container">

<div style="margin-top: 15px;"></div>
<div id="row">
<div id="column">
<div id="abc" onclick="switchMenu(this, event)">

<div id="columnHeader"><p>Section 1</p></div>

<div id="subSectionHeader"><p>Some Label I</p></div> <div id="subSectionHeader"><p>&nbsp;</p></div>
<div id="abcChild">
<div id="label"><p>Label</p></div> <div id="value"><p>Value</p></div>
<div id="label"><p>Label</p></div> <div id="value"><p>Value</p></div>
<div id="label"><p>Label</p></div> <div id="value"><p>Value</p></div>
</div>
</div> <!--abc-->

<!--cde -->
<div id="cde" onclick="switchMenu(this, event)">
<div id="subSectionHeader"><p>Some Label II</p></div> <div id="subSectionHeader"><p>&nbsp;</p></div>
<div id="cdeChild">
<div id="label"><p>Label</p></div> <div id="value"><p>Value</p></div>
<div id="label"><p>Label</p></div> <div id="value"><p>Value</p></div>
<div id="label"><p>Label</p></div> <div id="value"><p>Value</p></div>
</div>
</div>
<!--cde -->
</div>

</div>

</div>
</body>
</html>

您的建议和帮助将不胜感激感谢您的宝贵时间。

最佳答案

如果在子div上执行点击,则不执行操作。
更新:

编辑后的结构不同,因为你在 div 中有 p。我修改了代码以使用新结构。请注意,id 必须是唯一的。你的 css 打乱了布局并导致代码失败。如果你取出你的 css,代码就可以工作。

如果你使用 jquery,这会简单得多。

代码已更新以包含 RobG 的建议

更新2:修复了 findParent 中的错误,更加健壮。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Data</title>
<style>
#container {
clear: both;
width: 400px;
margin: 0 0;
border: none;
}

.row {
clear: both;
width: 100%;
border: none;
}

.column {
border: double #aca899;
float: left;
clear: none;
width: 402px;
}

.columnHeader p {
float: left;
clear: none;
text-align: center;
width: 100%;
height: 20px;
padding-top: 2px;
font-size: 8.0pt;
font-family: Verdana;
font-weight: bold;
color: black;
text-decoration: underline;
border: 1px ridge #aca899;
}

.subSectionHeader {
float: left;
clear: none;
width: 50%;
font-size: 7.5pt;
font-family: Arial;
background-color: #DBA083;
border: 1px ridge #aca899;
border-collapse: collapse;
padding: 1px;
}


.label {
float: left;
clear: none;
width: 50%;
font-size: 7.5pt;
font-family: Arial;
background-color: #F2CF93;
border: 1px ridge #aca899;
padding: 1px 1px 1px 11px;
}


.value {
float: left;
clear: none;
width: 50%;
font-size: 7.5pt;
font-family: Arial;
background-color: #DDDDDD;
border: 1px ridge #aca899;
padding: 1px;
}
</style>
<script type="text/javascript">
function switchMenu(cur, e) {
var obj = e.target || e.srcElement; //works on IE and FF
var childDiv = cur.id + 'Child';

if (obj.className.toLowerCase() != "nocollapse" && isInCollapsible(obj)) {
var el = document.getElementById(childDiv);
el.style.display = el.style.display != "none" ? "none" : "";
}
}

function isInCollapsible(obj) {
return findParent(obj, "div", "nocollapse") ? false : true;
}

function findParent(obj, type, name) {
if (obj.parentNode)
if (obj.parentNode.tagName && obj.parentNode.tagName.toLowerCase() == type && obj.parentNode.className.toLowerCase() == name)
return obj.parentNode;
else
return findParent(obj.parentNode, type, name);
else
return null;
}

</script>
</head>

<body>
<div id="container">

<div style="margin-top: 15px;"></div>
<div class="row">
<div class="column">
<div id="abc" onclick="switchMenu(this, event)">

<div class="columnHeader"><p>Section 1</p></div>

<div class="subSectionHeader"><p>Some Label I</p></div> <div class="subSectionHeader"><p>&nbsp;</p></div>
<div id="abcChild" class="noCollapse">
<div class="label"><p>Label</p></div> <div class="value"><p>Value</p></div>
<div class="label"><p>Label</p></div> <div class="value"><p>Value</p></div>
<div class="label"><p>Label</p></div> <div class="value"><p>Value</p></div>
</div>
</div> <!--abc-->

<!--cde -->
<div id="cde" class="parent" onclick="switchMenu(this, event)">
<div class="subSectionHeader"><p>Some Label II</p></div> <div class="subSectionHeader"><p>&nbsp;</p></div>
<div id="cdeChild" class="noCollapse">
<div class="label"><p>Label</p></div> <div class="value"><p>Value</p></div>
<div class="label"><p>Label</p></div> <div class="value"><p>Value</p></div>
<div class="label"><p>Label</p></div> <div class="value"><p>Value</p></div>
</div>
</div>
<!--cde -->
</div>

</div>

</div>
</body>
</html>

关于处理可折叠 div 和子 div 的 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6853454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com