[英]Click function show(), after next click it disappears
So I'm experiencing problem - I got a function, when someone clicks a menu, it will show a div tag. 所以我遇到了问题-我有一个功能,当有人单击菜单时,它将显示一个div标签。 See here -
看这里 -
$("a#cat").click(function() {
$("div#categoryBox").show();
return false;
});
So far everything works great, the div content shows up excellent, but the problem is that inside div content there are buttons (a tags), delete and edit, when I click one of these buttons, the div tag hides. 到目前为止,一切工作正常,div内容显示出色,但是问题是div内容内有按钮(一个标签),删除和编辑,当我单击这些按钮之一时,div标签隐藏了。 The button links are -
按钮链接是-
<a href="?action=edit&id=<?php echo $id; ?>"> and <a href="?action=delete&id=<?php echo $id; ?>">
If I press one of these links, the div content automatically hides, and I need to press again the a button with id #cat. 如果按下这些链接之一,则div内容将自动隐藏,我需要再次按下ID为#cat的a按钮。 Is there any way to make it stay, unless I press different menu link or refresh page?
有什么方法可以保留它,除非我按其他菜单链接或刷新页面?
If you need any additional information, please ask. 如果您需要任何其他信息,请询问。
May be the page is reloaded when you click on edit/delete links, so you think the div gets hidden. 单击“编辑/删除”链接时,页面可能会重新加载,因此您认为div被隐藏了。 If you are doing any client side implementation on
edit
and delete
click then you should make sure to prevent the default behavior or those links. 如果您要在客户端上执行任何
edit
和delete
,则应确保阻止默认行为或这些链接。 Try this. 尝试这个。
$('#categoryBox a').click(function(e){
e.preventDefault();
});
There are several ways to do this. 有几种方法可以做到这一点。 Perhaps the easiest is to re-show the div when the page loads if certain conditions are met.
如果满足某些条件,最简单的方法可能是在页面加载时重新显示div。
If you want to display the div every time the url is ?action=edit
or ?action=delete
, use this: 如果要在每次URL是
?action=edit
或?action=delete
时显示div,请使用以下命令:
$(function () {
if (/\baction=(edit|delete)\b/.test(location.search)) {
$("div#categoryBox").show();
}
});
Or, you could append a hash parameter when you want to show the div: 或者,当您要显示div时,可以附加一个哈希参数:
<a href="?action=edit&id=<?php echo $id; ?>#showCategoryBox">edit</a>
<a href="?action=delete&id=<?php echo $id; ?>#showCategoryBox">delete</a>
$(function () {
if (location.hash === "#showCategoryBox") {
$("div#categoryBox").show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.