简体   繁体   English

JavaScript不影响div

[英]javascript not affecting a div

I'm trying to build an FAQ page and wish to have the user click a question and only then an answer will side down under the question. 我正在尝试建立一个FAQ页面,希望用户单击一个问题,然后答案会朝下。 I have code that will work this when I put it all on one page. 当我将它们全部放在一页上时,我有可以使用的代码。 However I wish to use a div to load various files (the FAQ div is only one of a few files I wish to add into the div). 但是,我希望使用div来加载各种文件(FAQ div只是我希望添加到div中的几个文件之一)。 So when I use the div and an 'window onload event' the div loads but all questions and answers are fully opened and exposed. 因此,当我使用div和“ window onload事件”时,div会加载,但所有问题和答案都会完全打开并公开。 The script to dropdown answers, placed in the HEAD section of the MainPage.html also a call to jQuery. 下拉答案的脚本位于MainPage.html的HEAD部分中,它也是对jQuery的调用。

<script type="text/javascript">
$(document).ready(function() {
    $('#faqs h1').each(function() {
        var tis = $(this), state = false, answer = tis.next('div').hide().css('height','auto').slideUp();
        tis.click(function() {
            state = !state;
            answer.slideToggle(state);
            tis.toggleClass('active',state);
        });
    });
});
</script>

The FAQ.html FAQ.html

<div id="faqs">    
    <h1>+ Question</h1>
    <div>
        <p><br>Answer Here</p>
    </div><br>

    <h1>+ Question</h1>
    <div>
        <p><br>Answer Here</p>
    </div><br>

</div>

The script to load the div, placed in the HEAD section of the MainPage.html 加载div的脚本,位于MainPage.html的HEAD部分中

<script>
    window.onload = function(){
        $('#target').load('FAQ.html');
    }
</script>

The div, placed in the BODY section of the MainPage div,位于MainPage的BODY部分中

<div id="target"></div>

I have tried placing the javascript in various place on the page but still the same result. 我尝试将javascript放在页面上的不同位置,但结果仍然相同。 Repeat: If I place the code of FAQ.html directly in the BODY section of the MainPage it all works properly. 重复:如果我将FAQ.html的代码直接放置在MainPage的BODY部分中,则一切正常。

I have researched this also under 'conflicting jQuery' but with no success. 我也在“冲突的jQuery”下对此进行了研究,但没有成功。 Suggestions appreciated please. 建议表示赞赏。

It looks a lot like $(document).ready is run before $('#target').load finishes. 看起来很像$(document).ready$('#target').load完成之前运行。
To avoid that, take the entire function you pass to $(document).ready and pass it as a last argument to $('#target').load instead, so that it gets called when loading of FAQ.html has completed: 为避免这种情况,请将传递给$(document).ready的整个函数传递给$(document).ready $('#target').load作为最后一个参数 ,以便在FAQ.html加载完成时调用该函数

window.onload = function(){
    $('#target').load('FAQ.html', function() {
        $('#faqs h1').each(function() {
            var tis = $(this), state = false, answer = tis.next('div').hide().css('height','auto').slideUp();
            tis.click(function() {
                state = !state;
                answer.slideToggle(state);
                tis.toggleClass('active',state);
            });
        });
    });
}

I assume it's because of the 我认为是因为

$(document).ready()

callback. 打回来。 You're specifying a function to happen when the DOM is loaded, but by the time you've loaded in the faq.hmtl file, that's already happened . 您要指定一个函数,该函数在DOM加载时发生,但是到faq.hmtl文件中加载时,这已经发生了 See what happens if you remove the first and last lines inside your script tags. 查看如果删除脚本标记内的第一行和最后一行,会发生什么情况。


Also, unrelated, you're using <h1> tags incorrectly. 另外,不相关的是,您错误地使用了<h1>标签。 You should only have one <h1> tag per page, as a rule. 通常,每页只能有一个<h1>标记。 The <dl> tag is probably the most semantically meaningful in this situation. 在这种情况下, <dl>标记可能是最有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM