显示使用JavaScript隐藏
<body>
<input type="textbox" id="fname"></input>
<button type="button" onclick="javascript:show('select'); return false;">Click</button></br>
<input type="radio" id="select" style="display:none;"></input>
</body>
我写的代码似乎是合乎逻辑的,但在这种情况下,当我点击按钮,它似乎没有奏效。任何任何一个可以告诉我在哪里可以我已经错了显示使用JavaScript隐藏
在此先感谢。
<head>
<title>show and hide javascript</title>
<script type="text/javascript">
function show(id) {
//alert("Its working");
var myId = document.getElementById.id;
if (myId.style.display = 'none') {
myId.style.display = 'block';
} else {
myId.style.display = 'none';
}
}
</script>
</head>
改变这一行: 如果(myId.style.display = '无'){
到:
if(myId.style.display==='none'){
nope它不工作的方式要么... – 2012-07-16 19:33:00
这不是只有问题 – jfriend00 2012-07-16 19:37:18
我看到三个问题与你的函数:
1)将此行中的=
修改为==
:
if (myId.style.display == 'none') {
2)更改此:
document.getElementById.id;
这使其成为一个实际的函数调用:
document.getElementById(id);
3)使用一个跨浏览器getStyle()
功能,使您的样式值将包含CSS设置,而不仅仅是直接在对象上设置的值。直接从对象中读取样式属性,仅检索直接在对象上设置的样式值,不检索默认值。它不会反映通过CSS设置的内容。您可以实现跨浏览器功能从this article得到的样式属性包括哪些内容是通过这样的CSS设置:
function getStyle(el, cssprop){
if (el.currentStyle) //IE
return el.currentStyle[cssprop]
else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
return document.defaultView.getComputedStyle(el, "")[cssprop]
else //try and get inline style
return el.style[cssprop]
}
然后,您可以实现的功能是这样的:同样
function show(id) {
//alert("Its working");
var myId = document.getElementById(id)
if (getStyle(myId, "display") === "none") {
myId.style.display = 'block';
} else {
myId.style.display = 'none';
}
}
,因为你的功能实际上是一个切换,而不仅仅是一个节目,我建议将它重命名为toggle()
。您可能还想知道,这只会与块元素正常工作。如果在内联元素上尝试它,它会隐藏它,但是当它显示它时,它会将内联元素变成块元素。
非常感谢你...我只是一个初学者在javascr ipt ...Ur非常感谢:D – 2012-07-16 19:38:08
document.getElementById(id) // without the dot before parens
此代码似乎工作得很好,我们没有双重==建议。直到更改(id)和删除点才会起作用。
<html><head><title>test</title></head>
<body>
<script type="text/javascript">
function show(id){
//alert("Its working");
var myId=document.getElementById(id);
if(myId.style.display=='none'){
myId.style.display='block';
}
else{
myId.style.display='none';
}
}
</script>
<h1>Test</h1>
<input type="textbox" id="fname"></input>
<button type="button" onclick="javascript:show('select'); return false;">Click</button></br>
<input type="radio" id="select" style="display:none;" />
</body>
</html>
它首次正常工作,因为'='none''返回true,它切换到'block'。之后,它会一遍又一遍地将其更改为“阻止”。 '=='是必要的。 – 2012-07-16 19:33:06
我还删除了收音机上的结束标记。 – 2012-07-16 19:33:10
嘿,非常感谢你...这是我犯的错误..非常感谢你..appreciated你的帮助:D – 2012-07-16 19:34:11
function show(id) {
//alert("Its working");
var myId = document.getElementById(id);
if (myId.style.display == 'none') {
myId.style.display = 'block';
} else {
myId.style.display = 'none';
}
}
FTFY
'var myId = document.getElementById;'是错误的。 – jfriend00 2012-07-16 19:36:44
@ jfriend00大脑barf。谢谢! – 2012-07-16 19:39:22
你'如果(myId.style.display = 'none''应该是'==' none'' – bokonic 2012-07-16 19:25:13
我第一次见过'' – Musa 2012-07-16 19:27:37
我做了='没有'为=='没有',但仍然不工作 – 2012-07-16 19:27:52