gpt4 book ai didi

JQUery 隐藏 DIV 在 DIV 外部单击

转载 作者:行者123 更新时间:2023-12-01 08:19:31 24 4
gpt4 key购买 nike

假设我有以下控制权:

   <div class="slControlWrapper">
<div class="slControlLabel">
<asp:Label ID="lblSL" CssClass="lblSL" runat="server">Clickable Label</asp:Label>
</div>
<div class="slControlSeparator">
<img alt="" src="../Images/pxlGray.gif" style="width: 1px; height: 29px" />
</div>
<div class="slControlDropDown">
<img alt="Clickable DropDown image" src="../Images/btDropDownCharcoal.gif" style="width: 23px; height: 29px" />
</div>
<div id="wndSL">
This is the hidden content of my DIV Window
</div>
</div>

默认情况下,wndSL 有style=“显示:无;背景颜色:蓝色;高度:500px;宽度:590px;”

当我单击 slControlLabel 或 slControlDropDown 时,我能够显示 wndSL。但是,当我单击 slControlLabel 或 slControlDropDown 或 wndSL 之外的任何位置时,我还需要隐藏它。

任何人都可以帮助我弄清楚如何隐藏它,点击我的 wndSL 之外的任何地方。

这就是我到目前为止所拥有的......并且每当我在窗口外单击时,我需要添加该部分以隐藏它

谢谢

 $('.slControlLabel, .slControlDropDown').bind('click',function(event){
alert('This will open the wndSL');
$('#wndSL').show();
});

新问题修订版 2

这是我现在拥有的 DIV,正如您将看到的,我在那里有更多控件...所以原始答案不再起作用,因为当我单击 DIV 中的控件时,它仍然会关闭 DIV。 ...所以,虽然我最初的请求是当您在 DIV 外部单击时关闭,但当我在 DIV 内添加控件时,我的隐藏条件不起作用...我如何调整它,以便单击 DIV 内的任何内容不会关闭窗口(标题上的关闭按钮 icoRedX.gif 除外)

这是我的控制

    <div class="wndShoppingList" id="wndShoppingList" style="display: block; ">
<div id="ctl00_ctl00_phContent_DynamicUserControl_Step_1_pnlWndShoppingListContentPanel" style="display: block; "><div id="ctl00_phContent_DynamicUserControl_Step_1_pnlWndShoppingListContent">
<div class="tableContainer">
<div class="header">
<div class="slHeaderLeft">
Select a Saved Shopping List
</div>
<div class="slHeaderRight">
<a id="ctl00_phContent_DynamicUserControl_Step_1_btClose" class="RadButton RadButton_Default rbImageButton" href="javascript:void(0)" style="display:inline-block;height:20px;width:18px;background-image:url(/APPSERVER0001/Images/icoDeleteGrayX.gif);line-height:20px;line-height:20px;height:20px;"><span class="rbText rbHideElement" style="width:100%;padding-left:0;padding-right:0;"></span><input id="ctl00_phContent_DynamicUserControl_Step_1_btClose_ClientState" name="ctl00_phContent_DynamicUserControl_Step_1_btClose_ClientState" type="hidden" autocomplete="off"></a>
</div>
</div>
<div class="details">
<div class="gridDescription">
Select a saved Shopping List to open. The list you already have open will be automatically saved
</div>
<div class="gridContainer">
<div id="ctl00_ctl00_phContent_DynamicUserControl_Step_1_pnlTemplateShoppingListPanel">
<div id="ctl00_phContent_DynamicUserControl_Step_1_pnlTemplateShoppingList">
<div id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList" class="RadGrid RadGrid_Default" style="height:300px;" tabindex="0">
<table cellspacing="0" class="rgMasterTable" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00" style="width:100%;table-layout:auto;empty-cells:show;">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr style="display:none;">
<th scope="col">&nbsp;</th><th scope="col">&nbsp;</th><th scope="col">&nbsp;</th><th scope="col">&nbsp;</th>
</tr>
</thead><tbody>
<tr class="rgRow" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00__0">
<td><input type="image" name="ctl00$phContent$DynamicUserControl_Step_1$grdTemplateShoppingList$ctl00$ctl04$gbcSelectColumn" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl04_gbcSelectColumn" src="../Images/btSelectCharcoal.gif" style="border-width:0px;"></td><td>
<span id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl04_lblTemplateName">Master Shopping List</span><br>
<span id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl04_lblLastUpdated"> Updated August 10, 2011</span>
</td><td>16 items</td><td><input type="image" name="ctl00$phContent$DynamicUserControl_Step_1$grdTemplateShoppingList$ctl00$ctl04$gbcDeleteColumn" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl04_gbcDeleteColumn" src="../Images/icoDeleteRedX.gif" onclick="if(!confirm('Are you sure you want to delete this list ?'))return false;" style="border-width:0px;"></td>
</tr><tr class="rgAltRow" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00__1">
<td><input type="image" name="ctl00$phContent$DynamicUserControl_Step_1$grdTemplateShoppingList$ctl00$ctl06$gbcSelectColumn" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl06_gbcSelectColumn" src="../Images/btSelectCharcoal.gif" style="border-width:0px;"></td><td>
<span id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl06_lblTemplateName">Audrey's Birthday Party</span><br>
<span id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl06_lblLastUpdated">Shopped August 2, 2011 for $49.11</span>
</td><td>11 items</td><td><input type="image" name="ctl00$phContent$DynamicUserControl_Step_1$grdTemplateShoppingList$ctl00$ctl06$gbcDeleteColumn" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl06_gbcDeleteColumn" src="../Images/icoDeleteRedX.gif" onclick="if(!confirm('Are you sure you want to delete this list ?'))return false;" style="border-width:0px;"></td>
</tr><tr class="rgRow" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00__2">
<td><input type="image" name="ctl00$phContent$DynamicUserControl_Step_1$grdTemplateShoppingList$ctl00$ctl08$gbcSelectColumn" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl08_gbcSelectColumn" src="../Images/btSelectCharcoal.gif" style="border-width:0px;"></td><td>
<span id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl08_lblTemplateName">Monthly Household Supplies</span><br>
<span id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl08_lblLastUpdated">Updated February 22, 2011</span>
</td><td>8 items</td><td><input type="image" name="ctl00$phContent$DynamicUserControl_Step_1$grdTemplateShoppingList$ctl00$ctl08$gbcDeleteColumn" id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ctl00_ctl08_gbcDeleteColumn" src="../Images/icoDeleteRedX.gif" onclick="if(!confirm('Are you sure you want to delete this list ?'))return false;" style="border-width:0px;"></td>
</tr>
</tbody>
</table>
<input id="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ClientState" name="ctl00_phContent_DynamicUserControl_Step_1_grdTemplateShoppingList_ClientState" type="hidden" autocomplete="off">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

这是JS

function OnPageLoad(){
window.$ = $telerik.$;
var t;
$(document).ready(function () {
$('.slControlLabel, .slControlDropDown').bind('click',function(event){
$('#wndShoppingList').show();
event.stopPropagation();
});

// OnClick Event outside the wndShoppinglist.
// $('html').click(function(event){ Tried this as well
if((event.target.id != 'wndShoppingList')){ // This used to work but after adding controls in the DIV, this returns the ID of the controls within the DIV
alert('click on body '+ event.target.id)
alert('about to hide')
$('#wndShoppingList').hide();
});

}); //End $(document).ready()
}
Sys.Application.add_load(OnPageLoad);

最佳答案

将点击事件绑定(bind)到 html 以捕获所做的任何点击,并使其隐藏 #wndSL

$("html").click(function() {
$("#wndSL").hide();
});

然后使用 .stopPropagation(); 覆盖 #wndSL 的点击事件;

$("#wndSL").click(function (event) {
event.stopPropagation();
});

关于JQUery 隐藏 DIV 在 DIV 外部单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8162048/

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