简体   繁体   English

简单的Javascript在FireFox中不起作用(但在所有其他浏览器中都有效)

[英]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 其他一些考虑因素

  • I wouldn't use "display:none" for the answers for a simple yet perhaps not practical reason. 我不会使用“display:none”作为简单但可能不实际的原因的答案。 People without javascript enabled (as happen with the software of some handicapped people) will not be able to read the answers. 没有启用JavaScript的人(如某些残障人士的软件所发生的那样)将无法阅读答案。 This is why I hide the answers with javascript. 这就是我用javascript隐藏答案的原因。 They will only disappear if javascript is enabled. 只有启用了javascript,它们才会消失。
  • You can use other effects like animate(), slidetoggle() to make it more interesting. 您可以使用其他效果,如animate(),slidetoggle(),使其更有趣。

既然你已经有了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.

相关问题 javascript如何在FireFox中完美地工作,而在其他浏览器中却根本不工作? - How can javascript work perfectly in FireFox, but not work at all in other browsers? JPlayer在Firefox Android上不起作用,但在所有其他浏览器上 - JPlayer Does Not Work On Firefox Android But All Other Browsers Javascript firefox无法找到所有其他浏览器所做的值 - Javascript firefox can't find values all other browsers do this.type =&#39;password&#39;在IE中不起作用,但在其他所有浏览器中都不起作用 - this.type='password' doesn't work in IE, but all other browsers it does 带有Canvas的HTML5 / JavaScript文件无法在Windows 8上与Chrome和Firefox一起使用,但适用于Windows 7上的所有浏览器。为什么? - HTML5/JavaScript File with Canvas doesn't work with Chrome&Firefox on windows 8 but works with all browsers on windows 7. why? JavaScript:参数传递不适用于所有浏览器 - JavaScript: Argument passing doesn't work on all browsers 为什么这个简单的JavaScript在FireFox或Chrome中不起作用? - Why doesn't this simple JavaScript work in FireFox or Chrome? 谷歌地图使用Fancybox完美地运行Firefox,但其他浏览器不起作用 - Google Map using Fancybox working Firefox perfectly but Other Browsers it Doesn't Work 如何在Firefox以外的所有其他浏览器中实现此功能 - How to make this work in all other browsers other than Firefox 复选框输入在chrome中根本不起作用。 在其他浏览器中不起作用 - Checkbox Input doesn't work at all in chrome. Doesn't function in other browsers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM