简体   繁体   English

如何在第二个 HTML 页面中定义的一个 HTML 页面编辑元素中触发 JS function?

[英]How do I make a JS function triggered in one HTML page edit elements defined in a second HTML page?

Basically I have two HTML pages, and both are connected to the same JS file.基本上我有两个 HTML 页面,并且都连接到同一个 JS 文件。 I want a function triggered by an event handler element in the first HTML to edit an element in the second HTML page, and then open it.我想要一个由第一个 HTML 中的事件处理程序元素触发的 function 来编辑第二个 HTML 页面中的元素,然后打开它。

Here's a very basic example:这是一个非常基本的示例:

 $("#p1").click(function() { $("#p2el").text("TEST"); })
 <button id="p1">CLICK</button>

In this example, the button p1 is defined in the first HTML, and the p2el element is defined in a second HTML page, both linked to the same JS file.在此示例中,按钮p1定义在第一个 HTML 中, p2el元素定义在第二个 HTML 页面中,两者都链接到同一个 JS 文件。

In the above example, I also used location.href inside the function.在上面的例子中,我还在 function 中使用了location.href The idea being that, the element is edited, and automatically the second HTML page is loaded.想法是,元素被编辑,并自动加载第二个 HTML 页面。

It's not working, and I don't know why.它不起作用,我不知道为什么。 The second HTML page loads, but the element p2el is not edited.第二个 HTML 页面加载,但元素p2el未编辑。

I suspect this is has to do with data not transferring to the second HTML, but I am not sure why and what is happening exactly.我怀疑这与未传输到第二个 HTML 的数据有关,但我不确定为什么以及到底发生了什么。 I tried using localStorage inside the function, and then use the stored data as a condition that edits the element in the second HTML page...我尝试在 function 中使用localStorage ,然后使用存储的数据作为编辑第二个 HTML 页面中元素的条件...

 function second() { if(localStorage["key"] == "on") { $("#p2el").text("TEST"); location.href = "secondpage.html" } } $("#p1").click(function() { localStorage["key"] = "on"; second() })

... but It didn't work. ......但它没有工作。

I hope somebody can help me out.我希望有人可以帮助我。

Navigating to a new page completely resets the "JavaScript envirionment".导航到新页面会完全重置“JavaScript 环境”。

Your JS files are reloaded, everything starts anew.你的 JS 文件被重新加载,一切重新开始。 Some things persist through page loads, such as local storage and cookies, but function calls certainly don't.有些事情会通过页面加载持续存在,例如本地存储和 cookies,但 function 调用肯定不会。

To do what you want to do, you'll need to:要做你想做的事,你需要:

  1. Listen to the click event, and save the fact it was clicked somewhere.监听 click 事件,并保存它在某处被点击的事实。
    (You're already doing this) (你已经在这样做了)
  2. On page load, check the storage to determine whether or not the button was clicked at some time.在页面加载时,检查存储以确定是否在某个时间单击了按钮。 If it was, do whatever you want.如果是的话,做任何你想做的事。 You will probably want to reset the stored value so this happens only once.您可能希望重置存储的值,因此这只发生一次。

This will probably do the trick for you:这可能会为您解决问题:

if(localStorage["key"] === true) { 
  localStorage["key"] = false; // reset the key.
  $("#p2el").text("TEST");
}

$("#p1").click(function() { 
  localStorage["key"] = true;
  location.href = "secondpage.html"
});

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

相关问题 如何运行js函数并更改html页面? - How do I run a js function and change html page? 多页表单:如何根据请求无限期地重复一页? HTML,CSS,JS - Multi-page form: how do I repeat one page indefinitely by request? HTML, CSS, JS 如何使HTML页面上的所有相对URL都相对于其所在的其他域(在HTML元素以及JS HTTP请求中)? - How can I make all relative URLs on a HTML page be relative to a different domain then it's on (in HTML elements as well as JS HTTP Requests)? 如何使用 Js 获取 html 页面(元素) - how to fetch a html page(elements) with Js 我如何制作一个使用特定输入重定向到特定 html 页面的 js 代码? - How do i make a js code that redirects to a certain html page with a certain input? 如何在另一个HTML页面中的一个HTML页面中调用函数 - How to call a function in one html page in another html page 如何使此功能一次可用于我的html页面? - How do I make this function work for a single time for my html page? 如何使函数在html页面中自动运行? - How can I make a function run automatically in html page? 如何通过单击另一个html页面上的按钮来编辑页面? - How can I edit a page by clicking a button on the another html page? 如何在 HTML 的同一页面上制作多个幻灯片? - How do I make multiple slideshows on same page in HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM