我有一个表单,使用内联样式style="display: none;"
进行隐藏。
当页面上的链接被点击时,如何动态更改这个样式为style="display: inline;"
?
<a href="#" onclick="document.getElementById('myform').style.display = 'inline';">Click me</a>
jQuery是一个轻量级的JavaScript库,可以通过少量的脚本实现许多很酷的功能。
首先,我建议您阅读"jQuery如何工作?",它包含了您需要开始使用jQuery所需的所有信息。
我将解释一下我在fiddle中编写的代码。
首先是链接和表单。
<a id="linktotoggle" href="#">Click Me</a>
<form id="formtotoggle"></form>
document.getElementById()
一样。#formtotoggle { display: none; }
现在让我们编写jQuery代码
$(document).ready(function() {
// ^ This is an event, which triggers once all the document is loaded so that the manipulation is always guaranteed to run.
$("#linktotoggle").click(function() {
// ^ Attach a click event to our link
$("#formtotoggle").toggle();
// ^ select the form and toggle its display
});
});
希望这足以让您开始了解。
display = 'none'
来隐藏表单,而不是使用 display:inline
。 - Starx你需要先使用JavaScript找到一种选择表单的方法;以下是一个函数,假设你的表单具有id
属性为myform
:
function showForm() {
document.getElementById('myform').style.display = 'inline';
}
然后,只需将该函数绑定到您的链接的click
事件上。一种快速而简单的方法是将链接的onclick
属性设置为showForm(); return false
,但您可能希望在外部JavaScript中执行此操作,以便将内容和行为分开。
嘿,看看这个 JQuery。
用于展示。
$("#lnk").click(function () {
$("#result").removeAttr('Style');
$("#result").attr('Style','display: inline;'); // this
$("#result").attr('Style','display: block;'); // or this
});
隐藏
$("#lnk").click(function () {
$("#result").removeAttr('Style');
$("#result").attr('Style','display: none;');
});
这里是更多的代码
<a href="#" id="yourlink">yourlink</a>
<form id="yourform" style="display:none;">form here.</form>
<script>
$(document).ready(function () {
$('#yourlink').click(function () {
$('#yourform').css('display', 'inline');
});
});
</script>