[英]Toggle div on link click
http://jsfiddle.net/FsCHJ/2/ http://jsfiddle.net/FsCHJ/2/
what now happens is, whenever I have another link example it will also use this link as the toggle button. 现在发生的是,每当我有另一个链接示例时,它也会使用此链接作为切换按钮。 I just want Toggle Edit Mode to be toggling hidden div's on/off. 我只想要切换编辑模式来打开/关闭隐藏的div。 So I tried to change $("a").click(function ()
to $("toggle").click(function ()
and <a>Toggle Edit Mode</a>
to Toggle Edit Mode`, but doesn't work. Any idea's? 所以我尝试更改$("a").click(function ()
到$("toggle").click(function ()
和<a>Toggle Edit Mode</a>
以切换编辑<a>Toggle Edit Mode</a>
`,但不会'工作。任何想法?
HTML HTML
<li><a>Toggle Edit Mode</a>
</li>
<div class="hidden rightButton">hello</div>
CSS CSS
.hidden {
display: none;
}
.unhidden {
display: block;
}
JS JS
$(document).ready(function () {
$("a").click(function () {
$("div").toggleClass("hidden unhidden");
});
});
You want this. 你要这个。
<li><a class="toggle">Toggle Edit Mode</a>
$(".toggle").click(function () {
$("div").toggleClass("hidden unhidden");
}
You cannot use $("toggle")
, because this looks for the html tag <toggle>
. 你不能使用$("toggle")
,因为它会查找html标签<toggle>
。 Instead add a class toggle
to the link for which you want to toggle. 而是将类toggle
添加到要切换的链接。
Use "ID" selector. 使用“ID”选择器。
http://jsfiddle.net/FsCHJ/1/ http://jsfiddle.net/FsCHJ/1/
There can be many classes (class=...) in one page but juste on id (id=...) per page. 一个页面中可以有许多类(class = ...),但每页可以有id(id = ...)juste。 More informations here . 更多信息在这里 。
Javascript: 使用Javascript:
$(document).ready(function () {
$("#toggle").click(function () {
$("div").toggleClass("hidden unhidden");
});
});
Html: HTML:
<li><a id="toggle">Toggle Edit Mode</a></li>
<div class="hidden rightButton">hello</div>
$(document).ready(function () { $("#toggle").click(function () { $("div").toggleClass("hidden unhidden"); }); });
.hidden { display: none; } .unhidden { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <li><a id="toggle">Toggle Edit Mode</a></li> <div class="hidden rightButton">hello</div>
This worked for me. 这对我有用。 Allowing me to show or hide text with the same link. 允许我使用相同的链接显示或隐藏文本。 I associate the link with the div i want to show. 我将链接与我要显示的div相关联。 This works in lists with multiple records, each record having it's own ID. 这适用于具有多个记录的列表,每个记录都有自己的ID。
<a class="showHideToggle div1">View Details</a>
<div id="div1" style="display:none;">Record 1 details goes here</div>
<a class="showHideToggle div2">View Details</a>
<div id="div2" style="display:none;">Record 2 details goes here</div>
<script>
$(document).ready(function () {
$(".showHideToggle").click(function (ctl) {
var linkedDivName = $(this).attr('class').replace('showHideToggle ', '');
var divToToggle = $("#" + linkedDivName);
//alert('current status: ' + divToToggle.css('display'));
if (divToToggle.css('display') == 'none') {
divToToggle.css("display", "block");
$(this).text("Hide Details");
} else {
divToToggle.css("display", "none");
$(this).text("Show Details");
}
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.