1)如何编程(不需要编写onclick="javascript:..“属性)将JavaScript (jQuery)函数附加到下面的链接?
2)单击语句链接后,切换隐藏/取消隐藏的最简单方法是什么?第一次单击应该显示DIV (取消隐藏),第二次单击应该隐藏它,以此类推。
<a>Statement</a>
<div id="taxStatement">insert select statement dropdownbox</div>发布于 2010-09-11 01:41:09
您可以为链接指定一个类,例如:
<a class="toggle" href="#">Statement</a>
<div id="taxStatement">insert select statement dropdownbox</div>然后使用.click()在document.ready上附加脚本,并对元素执行.toggle()操作,如下所示:
$(function() {
$("a.toggle").click(function(e) {
$(this).next().toggle();
e.preventDefault();
});
});最初您可以通过多种方式隐藏<div>,CSS:
#taxStatement { display: none; }或者给它一个类,例如class="toggleDiv",然后以相同的方式隐藏它们:
.toggleDiv { display: none; }或者也可以通过脚本在您的document.ready中:
$(".toggleDiv").hide();发布于 2010-09-11 01:42:45
对于问题1和2,您需要使用切换:
$('a').toggle(
function () {
// Unhide Statement
$('#taxStatement').show();
},
function () {
$('#taxStatement').hide();
});发布于 2010-09-11 01:41:18
var theDiv = $('#taxStatement');
theDiv.hide();
// make the div hidden by default.
theDiv.prev('a').click(function(){ theDiv.toggle(); });
// ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
// Attach the onclick hide & unhide.
// assume the <a> is
// immediately before that <div>.
// It may be better to give an
// id to that <a>.https://stackoverflow.com/questions/3686869
复制相似问题