仅在点击特定链接后才在页面加载后执行JQuery

仅在点击特定链接后才在页面加载后执行JQuery

本文介绍了仅在点击特定链接后才在页面加载后执行JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个链接,点击后会导向另一个页面。我想在加载后在这个新页面上执行一些JQuery,但是仅当点击了特定链接才能进入页面



我有这个jQuery:

pre $ $ $ $'$'$ new $ to $('#new_to_topics')。click(function(){
$(document ).ready(function(){
$('#topic_guidelines')。slideDown('normal');
$('#topic_guidelines')。addClass('on');
});
});

其中 #new_to_topics 是链接,导致新页面和

  $('#topic_guidelines')。slideDown('normal'); 
$('#topic_guidelines')。addClass('on');

是我想在新页面上执行的JQuery代码。但是,这不起作用。我应该怎么做?

解决方案

您可以将位置哈希值传递到新页面,然后有条件地运行一些JavaScript如果我的链接是 mynewpage.html#fromXlink (这将显示在地址栏中)



我在mynewpage.html上的javascript可能是:

  $(如果(location.hash =='#fromXlink'){
$('#topic_guidelines')。slideDown('normal');
$ ('#topic_guidelines')。addClass('on');
}
});


I have a link in my app that when clicked, leads to another page. I want to execute some JQuery on this new page after it loads, but only if that specific link is clicked to get to the page.

I have this JQuery:

    $('#new_to_topics').click(function(){
        $(document).ready(function() {
            $('#topic_guidelines').slideDown('normal');
            $('#topic_guidelines').addClass('on');
        });
    });

where #new_to_topics is the id of the link that leads to the new page and

$('#topic_guidelines').slideDown('normal');
$('#topic_guidelines').addClass('on');

is the JQuery code I want to execute on that new page. However, this does not work. How should I do this?

解决方案

You could pass a location hash to the new page, and then conditionally run some javascript based on that hash.

If my link was to mynewpage.html#fromXlink (this would show in the address bar)

My javascript on mynewpage.html could be:

$(document).ready(function() {
  if (location.hash == '#fromXlink') {
    $('#topic_guidelines').slideDown('normal');
    $('#topic_guidelines').addClass('on');
  }
});

这篇关于仅在点击特定链接后才在页面加载后执行JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:14