更改另一个页面的元素属性 [英] Change element attribute of another page
问题描述
大家好,我的网站有问题.情况如下:
Hello guys I have some problem with my website. Here's the situation:
第1页
<script type="text/javascript" src="jscript.js">
<input type="button" value="change" onClick="changeAttr()">
第2页
<script type="text/javascript" src="jscript.js">
<input type="text" value="Hello" id="dynamictext">
jscript.js
jscript.js
function changeAttr(){
document.getElemenyById('dynamictext').value="World";
}
现在这两个页面在不同的选项卡上打开.我想发生的是,每当单击第1页上的按钮时,第2页上的输入文本的值将更改为世界".如何使用Javascript或Jquery做到这一点?
Now these 2 pages are open on different tabs. What I want to happen is whenever the button on page 1 is clicked, the value of input text on page 2 will change to "World". How can I make this possible with Javascript or Jquery?
推荐答案
第一个选项卡的任务是更改localStorage中的值.
localStorage.setItem('superValue', 'world');
The 1st tab has the task to change a value in localStorage.
localStorage.setItem('superValue', 'world');
同时第二个选项卡将监听"该localStorage值的更改:
Meanwhile the 2nd tab would be "listen" to changes on that localStorage value:
var dinamictext = document.querySelector('dinamictext');
setInterval(function () {
if (dinamictext.value !== localStorage['superValue']) {
dinamictext.value = localStorage['superValue'];
}
}, 100);
这当然仅适用于相同域中的页面.
This of course works only with pages on the same domain.
这篇关于更改另一个页面的元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!