简体   繁体   English

如何在不使用php的情况下使一个网页中的按钮的onclick函数在另一网页中执行功能

[英]How to make a button's onclick function in one webpage execute a function in another webpage without php

In my main html page, I have the following code: 在我的主要html页面中,我有以下代码:

<p class="text" id="time">00:00</p>

In my other html page, I have the following code: 在我的另一个html页面中,我有以下代码:

<button onclick="resettime()">RESET TIME</button>

They both link to the same javascript page, which has the code: 它们都链接到相同的javascript页面,该页面具有以下代码:

function timeIncrement()
{
    document.getElementById('time').innerHTML = +mins+":"+secs;
  if(secs==60)
  {
    mins+=1;
    secs = 0;
  }
}

var timecheck = setInterval(timeIncrement,1000);

function resettime()
{
  clearInterval(timecheck);
  mins  = 0;
  secs = 0;
  timecheck = setInterval(timeIncrement,1000);
}

Clicking the button doesn't reset the time to 0:0, so it doesn't work, so I was wondering if there's any way I can do this without going into php. 单击按钮不会将时间重置为0:0,所以它不起作用,所以我想知道是否有任何方法可以在不使用php的情况下进行此操作。 I'm new to html/js, so sorry if this is a repeat, couldn't find a similar one. 我是html / js的新手,所以很抱歉,如果重复一遍,找不到相似的内容。

Two html means two windows. 两个html表示两个窗口。 if you want to execute script from one window to another window, you should have the other window's object and both window should have document.domain. 如果要从一个窗口执行脚本到另一个窗口,则应具有另一个窗口的对象,并且两个窗口都应具有document.domain。 for having reference of another window 用于引用另一个窗口

in main html add the following 在主html中添加以下内容

<script>window.open("other.html");//other html will open automatically</script>

in other html, change the code to following 在其他html中,将代码更改为以下内容

<button onclick="resettimeinMainHtml()">RESET TIME</button>

<script>function resettimeinMainHtml(){window.opener.resettime();// will reset in mail html}</script>

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

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