简体   繁体   English

尝试修改显示/隐藏脚本,以便第一个div在页面加载时打开(Django项目)

[英]Trying to modify a show/hide script so first div opens on page load (Django project)

I'm working on a blog feed and have a script that currently opens up a div and displays it's (the div's) content onclick; 我正在处理博客供稿,并且具有一个脚本,该脚本当前打开一个div并在onclick上显示它(该div的)内容; however, I would like the first div in the feed to open when the page loads and right now I'm struggling to figure it out. 但是,我希望在页面加载时提要中的第一个div打开,现在我正在努力找出它。 I'm a serious django and javascript/jQuery noobie, so any help that can be offered with this is greatly appreciated. 我是一个认真的django和javascript / jQuery noobie,因此可以提供与此相关的任何帮助。

Currently everything else works. 目前,其他所有功能都可以使用。 The page loads the feed, all of the divs are closed and when selected they open up. 该页面将加载供稿,所有div均已关闭,并且在选中时它们会打开。

base.html base.html

<article   id="entry-{{ object.pk }}" {% if continue_reading %} onclick="toggleArticleActive('entry-{{ object.pk }}', true);" {% endif %}>

...blog post content

</article>

tools.js tools.js

function toggleArticleActive(article_dom_id, is_active) {
  if (is_active) { // deactivate all that are active
    $('.article-active').each(function(index, value) {
      toggleArticleActive($(this).attr('id'), false);
    });
  }

  var a = $('#' + article_dom_id);
  a.attr("class", is_active ? "article-active" : "");

  if (is_active) {
    a.find(".show-when-article-active").show(); // can animate, e.g. show('slow')
    a.find(".show-when-article-not-active").hide();

    // Load questions about this entry in the main div
    $('#div_activity').load('/f/?entry=' + article_dom_id.replace('entry-','') );
  } else {
    a.find(".show-when-article-active").hide();
    a.find(".show-when-article-not-active").show();
  }
}

You should just be able to show the div automatically when the .ready() event is fired. 当触发.ready()事件时,您应该只能够自动显示div。

$( document ).ready(function() {
    $("#firstdiv").show();
});

Where "#firstdiv" is the id of your first div in the list. 其中“ #firstdiv”是列表中第一个div的ID。 You can also select the first div using the ":first-child" selector of your containing element: 您也可以使用包含元素的“:first-child”选择器来选择第一个div:

http://api.jquery.com/first-child-selector/ http://api.jquery.com/first-child-selector/

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

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