[英]Simple Javascript doesn't work in FireFox (but does in ALL other Browsers)
I have this code which I'm using to reveal the Answers to some questions in an FAQ section on my website. 我有这个代码,我用它来在我的网站上的FAQ部分中显示一些问题的答案。 You click the question and the div containing the answer is revealed below.
单击该问题,下面显示包含答案的div。
It works in Safari, Chrome, Opera and even IE, but not in Firefox. 它适用于Safari,Chrome,Opera甚至IE,但不适用于Firefox。
The links just do nothing. 这些链接什么都不做。
Any ideas why, or any better methods for doing an 'FAQ' section? 任何想法,或任何更好的方法来做“常见问题”部分? I have already got jQuery loaded in my page if there's something that could be done better with it there, I don't know.
我已经在我的页面中加载了jQuery,如果在那里可以做得更好,我不知道。 Or a CSS only solution?
还是仅限CSS的解决方案?
Here is my code: 这是我的代码:
JS code: JS代码:
<script type="text/javascript">
function toggle(tag) {
if (tag.style.display=='') {
tag.style.display='none'
} else {
tag.style.display=''
}
}
</script>
HTML code: HTML代码:
<a href="javascript: toggle(question1)">FAQ Question goes here</a>
<div id="question1" style="display:none">
<p>FAQ Answer goes here</p>
</div
Because you set the ID of the element you want to toggle, you would need to use: 因为您设置了要切换的元素的ID,所以您需要使用:
document.getElementByID(tag).style.display
instead of just tag.style.display
inside of your toggle function. 而不仅仅是你的切换功能中的
tag.style.display
。
尝试将显示设置为“阻止”
tag.style.display='block'
A simpler way to do this is: 一种更简单的方法是:
<script type="text/javascript">
function toggle(tag) {
tag.style.display = (tag.style.display == 'block') ? 'none' : 'block';
}
</script>
Ok there are a number of issues in your code 好吧,您的代码中存在许多问题
First, it is important to end your lines with a semi-colon. 首先,用分号结束你的线是很重要的。
tag.style.display='none'
should be tag.style.display='none';
tag.style.display='none'
应为tag.style.display='none';
Second, You must use document.getElementById on the id to get the DOM Element 其次,您必须在id上使用document.getElementById来获取DOM元素
I would do somthing like this: 我会做这样的事情:
function toggle(elementId) {
var tag = document.getElementById(elementId);
...
}
Here is a demo http://jsbin.com/abuce4/edit 这是一个演示 http://jsbin.com/abuce4/edit
Seriously, I recommend you use jquery, you will not have this kind of problem and it's less than 30 Kbs. 说真的,我建议你使用jquery,你不会遇到这种问题而且它不到30 Kbs。
Here is what you look for: 这是你寻找的:
http://api.jquery.com/toggle/ http://api.jquery.com/toggle/
Anyway, you should always use: document.getElementByID
to get a certain element. 无论如何,您应该始终使用:
document.getElementByID
来获取某个元素。
EDITED EDITED
It depends on the structure of your HTML, if I had this situation: 这取决于HTML的结构,如果我遇到这种情况:
http://www.jsfiddle.net/dactivo/Qcm4G/ http://www.jsfiddle.net/dactivo/Qcm4G/
I would do this: 我会这样做:
$(".questionsheader").click(function(){
$(this).next("div").toggle(); }
);
SOME OTHER CONSIDERATIONS 其他一些考虑因素
既然你已经有了jQuery,试试这个:
$("#" + tag).toggle();
尝试<a onclick='toggle(document.getElementById("question1"));'">
Try this (quotes added): 试试这个(添加了引号):
<a href="javascript: toggle('question1')">FAQ Question goes here</a>
<div id="question1" style="display:none">
<p>FAQ Answer goes here</p>
</div>
and (used getElementById): 和(使用getElementById):
<script type="text/javascript">
function toggle(tag) {
tag = document.getElementById(tag);
if (tag.style.display=='') {
tag.style.display='none'
} else {
tag.style.display=''
}
}
</script>
UPDATES : for the jQuery solution, I would use the jQuery Tabs approach: 更新 :对于jQuery解决方案,我会使用jQuery Tabs方法:
<a class="toggle" href="#question1">FAQ Question Link 1</a>
<div id="question1" style="display:none">
<p>FAQ Answer goes here</p>
</div>
<a class="toggle" href="#question2">FAQ Question Link 2</a>
<div id="question2" style="display:none">
<p>FAQ Answer goes here</p>
</div>
and the JS: 和JS:
<script type="text/javascript">
$(document).ready(function() {
$('.toggle').click(function() {
$($(this).attr('href')).toggle();
return false;
});
});
</script>
<a href="#" onclick="javascript:$(this).next().toggle();">Question</a>
<div style="display:none;">Answer</div>
Or more 'generic' and clean : 或者更“通用”和清洁:
HTML : HTML:
<a class="question">Question 1</a>
<div class="answer">Answer 1</div>
<a class="question">Question 2</a>
<div class="answer">Answer 1</div>
CSS : CSS:
.answer{
display:none;
}
JS : JS:
$(document).ready(function() {
$(".question").click(function(event){
$(this).next().slideToggle();
});
}
slideToggle(); 的slideToggle(); or toggle();
或toggle(); Enjoy.
请享用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.