更改另一个页面中另一个div的CSS属性 [英] Changing CSS proprety of another div located in another page
问题描述
您好,我想更改html页面的DOM,但是从另一个页面我尝试在2个页面之间共享相同的脚本,但是在这里没有用,这是我要执行的操作的说明
Hello i would like to change the DOM of an html page but from another page i tried sharing the same script between the 2 pages but it didnt work here is an illustration for what i am trying to do
1-首页
<div>
<p id="simpletext">Hello world</p>
<script src="jquery.min.js"></script>
<script src="script.js" type ="text/javascript"></script> <!-- same script file is present in the second page -->
</div>
2-第二页
<div>
<button>Click me </button>
<script src="jquery.min.js"></script>
<script src="script.js" type ="text/javascript"></script><!-- same script file is present in the first page -->
</div>
script.js
$("button").on("click",function(){
$("simpletext").css("color","blue");
});
当我单击第二页上的希望按钮时,我试图更改第一页上文本的颜色
i am trying to when i click on the button wish is located on the second page to change the color of the text on the first page
推荐答案
您可以将消息从一页发送到另一页.在按钮一侧发送一条消息:
You can send messages from one page to another. At the buttons side send a message:
$("button").on("click",function(){
wondow.postMessage("show","*");
});
然后在另一页上,等待消息:
Then at the other page, wait for a message:
window.addEventListener("message",function(event){
if(event.data === "show") {
$("simpletext").css("color","blue");
}
});
我假设两个窗口都在同一台计算机上打开,否则,您需要使用ajax或websockets并进行服务器端代理.
I assume that both windows are opened on the same machine, if not, you need ajax or websockets instead and do serverside proxying.
这篇关于更改另一个页面中另一个div的CSS属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!