Javascript django jquery滚动未激活

Javascript django jquery滚动未激活,javascript,jquery,html,django,Javascript,Jquery,Html,Django,我有一个jquery函数,当我将其作为事件运行时,它可以工作,但当我试图在文档重新加载时让它运行时,它不工作 这是页面刷新时窗口滚动到的我的元素 {% block content %} {% for poll in polls %} <button class="test" id="{{ poll.id }}">{{ poll.id }}</button> {% endfor %} {% endblock %} {%block content%} {

我有一个jquery函数,当我将其作为事件运行时,它可以工作,但当我试图在文档重新加载时让它运行时,它不工作

这是页面刷新时窗口滚动到的我的元素

{% block content %}
  {% for poll in polls %}

    <button class="test" id="{{ poll.id }}">{{ poll.id }}</button>

  {% endfor %}
{% endblock %}
{%block content%}
{%用于轮询中的轮询%}
{{poll.id}
{%endfor%}
{%endblock%}
下面的代码工作正常,它附加到单击事件

{% block jquery %}
{#  <script>#}

testEvent();

  // sending the poll id to report after the flag is clicked on
  function testEvent() {
    $(".test").click(function(event){
      event.preventDefault();

      var scrolltoid = document.getElementById('54')
      scrolltoid.scrollIntoView(true);
      window.scrollBy(0, -50);

    });
  };

{#  </script>#}
{% endblock %}
{%block jquery%}
{#  #}
testEvent();
//单击标志后发送要报告的轮询id
函数testEvent(){
$(“.test”)。单击(函数(事件){
event.preventDefault();
var scrolltoid=document.getElementById('54')
scrolltoid.scrollIntoView(true);
滚动窗口(0,-50);
});
};
{#  #}
{%endblock%}
我需要代码在页面刷新时工作。但是,当相同的代码插入到document ready函数中,以便在页面刷新时将窗口滚动到特定元素时,它将不起作用

{% block jquery %}
{#  <script>#}


$(document).ready(function(){
    var scrolltoid = document.getElementById('54')
    scrolltoid.scrollIntoView(false);
})


{#  </script>#}
{% endblock %}
{%block jquery%}
{#  #}
$(文档).ready(函数(){
var scrolltoid=document.getElementById('54')
scrolltoid.scrollIntoView(false);
})
{#  #}
{%endblock%}
我还尝试运行下面的替代代码。我看到滚动很快就成功了,但当页面完全刷新时,我的窗口会再次滚动到顶部,而不是切换到元素id

{% block jquery %}
{#  <script>#}
  $(document).ready(function () {
      // Handler for .ready() called.
      $('html, body').animate({
          scrollTop: $('#54').offset().top
      }, 'slow');
  });
{#  </script>#}
{% endblock %}
{%block jquery%}
{#  #}
$(文档).ready(函数(){
//调用了.ready()的处理程序。
$('html,body')。设置动画({
scrollTop:$('#54').offset().top
}“慢”);
});
{#  #}
{%endblock%}

这段代码是前一位程序员留下的,我对{##}的用法感到困惑。我认为我的代码无法工作的原因可能是因为其中包含了{##}。我对它的用法感到非常困惑,如果我删除注释,页面上的所有jquery/javascript都将停止运行,因此我无法删除它。但这让我感到困惑,因为{##}是用来注释django上的代码,注释掉的内容不应该影响代码本身,但是我的代码中的{##}标记是让页面上的所有脚本都工作所必需的(除非在页面刷新时它不与scroll一起工作)。我试过对{##}进行研究,但没有发现任何有用的东西。

wrt/注释的“script”标记:您的模板扩展了另一个,显然已经有了开始和结束“script”标记-您可以通过找到。实际上,将一个模板扩展到另一个模板的脚本标记使用“%”-{%block content%},我有疑问的脚本标记使用“#”,它用于注释文档中描述的代码。我知道django模板语言thx。请重新阅读我之前的评论,然后检查父模板,直到找到
jquery
块的初始声明。您可以确保html
script
标记将在块的周围。这就是为什么在子模板中取消对它们的注释会破坏所有剩余的js代码,并出现语法错误-html标记不是有效的js。