Javascript 更改另一页上div的内容
在第1页,我有一个包含信息的Javascript 更改另一页上div的内容,javascript,jquery,Javascript,Jquery,在第1页,我有一个包含信息的div <div id='information'></div> 现在我想要的是,当我单击submit按钮时,文本区域中输入的文本将发布在div#information中,更改其以前的内容。 我看过很多其他关于如何更改div内容的帖子,但这些都与我的问题无关。你是说这样的事情吗 $("#submit-info").click(function() { var content = $("#new-info").text(); $("
div
<div id='information'></div>
现在我想要的是,当我单击submit按钮时,文本区域中输入的文本将发布在div#information
中,更改其以前的内容。我看过很多其他关于如何更改div内容的帖子,但这些都与我的问题无关。你是说这样的事情吗
$("#submit-info").click(function() {
var content = $("#new-info").text();
$("#information").html(content);
});
如果你想了解服务器端的情况,请多谈谈你所使用的技术。你是说这样的事情吗
$("#submit-info").click(function() {
var content = $("#new-info").text();
$("#information").html(content);
});
如果你想了解服务器端的情况,请详细介绍一下你所使用的技术。希望这能让你了解如何做到这一点: 第2页 HTML
<form>
<textarea id='new-info'></textarea>
<input type='submit' id='submit-info' value='pass'/>
</form>
<div id='information'>
</div>
保存_data.php
<?php
if (isset($_POST['new-info'])) {
// Update value in DB
}
?>
希望这能让您了解如何做到这一点: 第2页 HTML
<form>
<textarea id='new-info'></textarea>
<input type='submit' id='submit-info' value='pass'/>
</form>
<div id='information'>
</div>
保存_data.php
<?php
if (isset($_POST['new-info'])) {
// Update value in DB
}
?>
一种方法是像其他答案所提到的那样,让每个选项卡与一个中央服务器通信,该服务器将获取/发送数据,以使用AJAX保持两个选项卡的更新 但我在这里告诉你们另一种方法,就是使用我们已经为这类任务设计的东西。所谓的浏览器
localStorage
浏览器存储的工作原理类似于以下伪代码:
//set the value, it works as a hash map or assoc array.
localStorage .setItem("some_index_key", "some data") ;
// get the value by it's index key.
localStorage .getItem("some_index_key") ; // will get you "some data"
所有数据将在同一域的所有打开选项卡之间共享。您可以添加事件监听器,这样每当一个值更改时,它都会反映在所有选项卡上
addEvent(window, 'storage', function (event) {
if (event.key == 'some_index_key') {
output.innerHTML = event.newValue;
}
});
addEvent(myInputField, 'keyup', function () {
localStorage.setItem('some_index_key', this.value);
});
检查此项,您可以在A页编辑一个字段,该值将脱机反映在B页上,而无需加重网络负担。
要了解更多
真实的实例。背景色由另一个选项卡控制。
var screenone=document.getElementById('screenone');
screenone.addEventListener('keydown',screenone函数);
screenone.addEventListener('change',screenone函数);
函数ScreenOne函数()
{
document.body.style.backgroundColor=this.value;
setItem(“color1”,this.value);
}
var screentwo=document.getElementById('screentwo');
屏幕2.addEventListener('keydown',函数(evt){
setItem(“color2”,this.value);
});
屏幕2.addEventListener('change',函数(evt){
setItem(“color2”,this.value);
});
var thebutton=document.getElementById('thebutton');
按钮。addEventListener('click',函数(evt){
localStorage.clear();
screenone.value=“”;
screentwo.value=“”;
document.body.style.backgroundColor=“”;
});
var storageHandler=函数(){
document.body.style.backgroundColor=localStorage.color2;
var color1=localStorage.color1;
var color2=localStorage.color2;
screenone.value=color2;
screentwo.value=color1;
};
addEventListener(“存储”,storageHandler,false)代码>
.screenone{边框:1px纯黑色;}
输入{边距:10px;宽度:250px;高度:20px;边框:圆形}
标签{margin:15px;}
键入颜色名称,例如红色。或输入颜色十六进制代码,例如#001122
此选项卡
其他选择的选项卡
一种方法是像前面提到的其他答案一样,让每个选项卡与一个中央服务器通信,该服务器将获取/发送数据,例如使用AJAX更新两个选项卡
但我在这里告诉你们另一种方法,就是使用我们已经为这类任务设计的东西。所谓的浏览器localStorage
浏览器存储的工作原理类似于以下伪代码:
//set the value, it works as a hash map or assoc array.
localStorage .setItem("some_index_key", "some data") ;
// get the value by it's index key.
localStorage .getItem("some_index_key") ; // will get you "some data"
所有数据将在同一域的所有打开选项卡之间共享。您可以添加事件监听器,这样每当一个值更改时,它都会反映在所有选项卡上
addEvent(window, 'storage', function (event) {
if (event.key == 'some_index_key') {
output.innerHTML = event.newValue;
}
});
addEvent(myInputField, 'keyup', function () {
localStorage.setItem('some_index_key', this.value);
});
检查此项,您可以在A页编辑一个字段,该值将脱机反映在B页上,而无需加重网络负担。
要了解更多
真实的实例。背景色由另一个选项卡控制。
var screenone=document.getElementById('screenone');
screenone.addEventListener('keydown',screenone函数);
screenone.addEventListener('change',screenone函数);
函数ScreenOne函数()
{
document.body.style.backgroundColor=this.value;
setItem(“color1”,this.value);
}
var screentwo=document.getElementById('screentwo');
屏幕2.addEventListener('keydown',函数(evt){
setItem(“color2”,this.value);
});
屏幕2.addEventListener('change',函数(evt){
setItem(“color2”,this.value);
});
var thebutton=document.getElementById('thebutton');
按钮。addEventListener('click',函数(evt){
localStorage.clear();
screenone.value=“”;
screentwo.value=“”;
document.body.style.backgroundColor=“”;
});
var storageHandler=函数(){
document.body.style.backgroundColor=localStorage.color2;
var color1=localStorage.color1;
var color2=localStorage.color2;
screenone.value=color2;
第二,价值观