【发布时间】:2011-12-15 00:59:14
【问题描述】:
我有一个调用#notify div 的函数。
当用户点击这个 div 时,该函数被调用。然后当用户再次点击同一个 div 时,该功能被隐藏(使用切换)。
我想知道如何使用这个相同的功能,而是有一个关闭点击元素,所以如果用户关闭点击,则 div 被隐藏。
简而言之 当用户单击 div(小框)时,会调用一个函数(小框下方会出现一个大框),删除大框的唯一方法是再次单击小框。我希望当用户点击大框外的任何位置时隐藏大框。
<script>
$(document).ready(function() {
var notify = $("#notify");
var notifyLink = $("#notify_link");
var result = $("#result");
var loader = $('#loader');
var count = $("#count");
notify.hide();
notify.click(function(event) {
// Handle the click on the notify div so the document click doesn't close it
event.stopPropagation();
});
notifyLink.click(
function () { notify.toggle(notify.css('display') == 'none');
loader.html('<?php echo elgg_view('ajax/loader',array('slashes' => true)); ?>');
result.load("<?php echo $vars['url']; ?>mod/notifications/ajax/data.php",function(){
loader.empty(); // remove the loading gif
});
});
notifyLink.toggle(
function () {
$(this).addClass("selected");
},
function () {
$(this).removeClass("selected");
} );
count.load("<?php echo $vars['url']; ?>mod/notifications/ajax/livenum.php");
var refreshId = setInterval(function() {
count.load("<?php echo $vars['url']; ?>mod/notifications/ajax/livenum.php");
}, 2500);
$.ajaxSetup({ cache: false });
});
</script>
<a href="#" id="notify_link"> </a><span id="count"></span>
<div id="notify" style="display:none;">
<h4>Notifications</h4>
<div id="loader"></div>
<div id="result" style="width:100%; height:100%;"></div>
<div class="seeall"><a href="<?php echo $vars['url']; ?>mod/notifications/all.php">See All Notifications</a></div>
</div>
【问题讨论】:
-
您也可以提供 HTML 正文吗?而且我对您的要求感到困惑,您能否详细说明“有一个关闭点击元素,因此如果用户关闭点击,则隐藏 div”?
-
当用户点击 div(小框)时,会调用一个函数(小框下方会出现一个大框),移除大框的唯一方法是再次点击小框。当用户单击大框外的任何位置时,我希望大框隐藏。我希望这是有道理的。
-
对代码进行了格式化,使其更具可读性。由于 jsbeautifier 没有忽略嵌入在 php 标记中的
'标记,因此必须经过多次传递。 -
标记为收藏,稍后再查看。如果你自己解决了这个问题,请在我回复之前告诉我。