【问题标题】:Need to fire an alert after "page load" on same page需要在同一页面上的“页面加载”后触发警报
【发布时间】:2016-03-11 03:03:02
【问题描述】:

这是一个 WordPress 网站,我有一个模板可以在此 URL http://www.example.com/articles 的列表中呈现所有帖子。当用户单击帖子标题时,它会使用相同的模板但使用不同的 URL http://www.example.com/articles/?article_id=298 加载帖子的内容。我不知道如何在页面加载帖子内容后显示警报。我试过setTimeout$(document).ready$(window).loadwindow.onload=funtion(),现在$(document).on("pagecontainerload",function()

JQuery/JavaScript

    $('a.open-alert').click(function() {
      $(document).on("pagecontainerload",function(){
          window.alert('hey');
      });
    });

HTML

            <?php
                $tearsheet = get_query_var('article_id');
                if($tearsheet != '') {
                    echo 'there is something here';
                } else {

                    $args = array(
                    'post_type' => 'post',
                    'posts_per_page' => -1,
                    );

                    $query = new WP_Query( $args );

                    if($query->have_posts()) :
                        while($query->have_posts()) :
                            $query->the_post();
                ?>
                            <div>
                                <?php $id = get_the_ID(); ?>
                                <?php $title = get_the_title(); ?>
                                <a class="open-alert" href="<?php bloginfo('url'); echo '/find-articles/?article_id='.$id; ?>"><?php echo $title; ?></a>
                            </div>
                <?php
                        endwhile;
                    endif;
                }
            ?>

【问题讨论】:

  • 当打开警报链接被点击时,整个页面是否重新加载(使用 href-attribute 中的 URL)?
  • 是的,整个页面都重新加载了。

标签: javascript jquery


【解决方案1】:

click 处理程序永远不会被执行,因为页面卸载并加载了一个新页面 - 链接 &lt;a class="open-alert" href="&lt;?php bloginfo... 中的那个。您可能在 onclick 处理程序中有一个return false,但这意味着该链接不会被执行。

为了动态执行此操作,请考虑仅重新加载该页面中的容器,其中包含一些 Ajax 功能的帖子文本。

【讨论】:

  • 谢谢!完美运行。我还是新手,所以我还不能投票,但非常感谢。
猜你喜欢
  • 1970-01-01
  • 2017-08-06
  • 2019-03-31
  • 2011-09-07
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
相关资源
最近更新 更多