从另一页面上的链接调用一个页面上的函数

时间:2022-03-17 15:59:09

I have an html page (django) that contains several divs(each displaying different data) and one div that has navigation links. I'll call this my main page. I have an external .js file (jQuery) that reveals one display div on the main page and simultaneously hides all of the others (except the navigation div) based on which nav link is chosen.

我有一个html页面(django),其中包含几个div(每个显示不同的数据)和一个具有导航链接的div。我将这称为我的主页。我有一个外部.js文件(jQuery),它在主页面上显示一个显示div,同时根据选择的导航链接隐藏所有其他(导航div除外)。

$(function(){

$("#sl_sectorbr").click(function showSectorMini(){
  $(".minimenu").hide();
  $(".minimenu > *").hide();
  $("#sector_mini").fadeIn("slow");
  $("#sector_mini > *").fadeIn("slow");
});

All of this works fine. My question is, if I want to place "navigation links" on a second html page; that when clicked would both load my main page AND call/execute a specific function like "showSectorMini()" just as if it were clicked from the main page itself — how would I write that code? I'd really appreciate any help I could get.

所有这一切都很好。我的问题是,如果我想在第二个html页面上放置“导航链接”;单击时将加载我的主页并调用/执行像“showSectorMini()”这样的特定函数,就好像它是从主页本身单击一样 - 我将如何编写该代码?我真的很感激我能得到的任何帮助。


Oh.... existing class is...

哦....现有的班级是......

}
/* ------- Interaction Containers Class -------- */
.interactContainers {
padding:8px;
border:#999 1px solid;
display:none;
}

But you probably already knew that!

但你可能已经知道了!

Steve


Gentlemen...

This is exactly what I need and mine is less complicated than this. Just one div to open with the script. I am a javascript incompetent person so far. :(

这正是我所需要的,而且我的不那么复杂。用脚本打开一个div。到目前为止,我是一个无能为力的人。 :(

How do you dumb this down to my needs?

你是如何根据我的需要愚蠢的?

Seperate page link is: <a href="myprofile.php?id='. $id .'" target="_parent">Email</a></div>

单独的页面链接是:电子邮件

Page it goes to code is:

它转到代码的页面是:

function toggleSlideBox(x) {
    if ($('#'+x).is(":hidden")) {
            $(".interactContainers").slideUp(200);
            $('#'+x).slideDown(300);

    } else {
        $('#'+x).slideUp(300);
    }
}

and the div is this...

而div就是这个......

<div class="interactContainers"  id="interactContainers" style="background-color: #EAF4FF;">

I just want to click the link (Email) from one page...have it open the correct persons(id) profile page...and then execute my existing toggleSlideBox javascript.

我只是想从一个页面点击链接(电子邮件)...让它打开正确的人(id)个人资料页...然后执行我现有的toggleSlideBox javascript。

Is that doable without a bunch or re-code with javascript that I have about an IQ of 4 in. :\

如果没有一堆或重新编码我的智商为4英寸的javascript,这是可行的::\

Thank you for any assistance you provide

感谢您提供的任何帮助

S

3 个解决方案

#1


You could use the hash - link to http://example.com/#sectionOne and read the hash in your ready function.

您可以使用哈希链接到http://example.com/#sectionOne并读取就绪函数中的哈希值。

#2


As SidneySM suggested, a hash is the standard way of handling this. It could go something like this:

正如SidneySM所建议的那样,哈希是处理这个问题的标准方法。它可能会像这样:

In your external js file:

在您的外部js文件中:

var initSectorUI = function(){
  if (location.hash) showSectorMini(location.hash);
};

var showSectorMini = function(sector){
  $('#sectors > div').hide();
  $(sector).show();
};

On your other pages:

在您的其他页面上:

$(function(){
  initSectorUI();

  $("#navigator a").click(function(){ 
    showSectorMini($(this).attr('href'));
  });
});

<div id="navigator">
  <a href="#one">One</a>
  <a href="#two">Two</a>
</div>
<div id="sectors">
  <div id="one" style="display:none">A one</div>
  <div id="two" style="display:none">A two</div>
</div>

#3


You should arrange to produce different versions of the page, and put different onload actions into each version. For example, make the div to show a query parameter, and make django fill in the right onload depending on the query parameter. Then put the different query parameters into the links.

您应该安排生成不同版本的页面,并在每个版本中添加不同的onload操作。例如,使div显示查询参数,并根据查询参数使django填充正确的onload。然后将不同的查询参数放入链接中。

#1


You could use the hash - link to http://example.com/#sectionOne and read the hash in your ready function.

您可以使用哈希链接到http://example.com/#sectionOne并读取就绪函数中的哈希值。

#2


As SidneySM suggested, a hash is the standard way of handling this. It could go something like this:

正如SidneySM所建议的那样,哈希是处理这个问题的标准方法。它可能会像这样:

In your external js file:

在您的外部js文件中:

var initSectorUI = function(){
  if (location.hash) showSectorMini(location.hash);
};

var showSectorMini = function(sector){
  $('#sectors > div').hide();
  $(sector).show();
};

On your other pages:

在您的其他页面上:

$(function(){
  initSectorUI();

  $("#navigator a").click(function(){ 
    showSectorMini($(this).attr('href'));
  });
});

<div id="navigator">
  <a href="#one">One</a>
  <a href="#two">Two</a>
</div>
<div id="sectors">
  <div id="one" style="display:none">A one</div>
  <div id="two" style="display:none">A two</div>
</div>

#3


You should arrange to produce different versions of the page, and put different onload actions into each version. For example, make the div to show a query parameter, and make django fill in the right onload depending on the query parameter. Then put the different query parameters into the links.

您应该安排生成不同版本的页面,并在每个版本中添加不同的onload操作。例如,使div显示查询参数,并根据查询参数使django填充正确的onload。然后将不同的查询参数放入链接中。