Javascript 如何从另一个HTML页面获取textarea输入
在a.html中: 我有一个文本区域,在用户单击submit按钮后转换为链接。当用户点击链接时,他们被重定向到b.htmlJavascript 如何从另一个HTML页面获取textarea输入,javascript,html,Javascript,Html,在a.html中: 我有一个文本区域,在用户单击submit按钮后转换为链接。当用户点击链接时,他们被重定向到b.html <textarea id="sentenceId"> </textarea> <br> <button type="button" id="buttonId" onclick="createLink(document.getElementById('sentenceId').value)">Submit </button
<textarea id="sentenceId">
</textarea>
<br>
<button type="button" id="buttonId" onclick="createLink(document.getElementById('sentenceId').value)">Submit
</button>
<p id="demo">
<a id ="link" href="b.html"></a>
</p>
使用本地存储
该属性允许您访问本地存储对象。本地存储类似于会话存储。唯一的区别是,虽然localStorage中存储的数据没有过期时间,但在浏览会话结束时(即浏览器关闭时),sessionStorage中存储的数据将被清除
a.html
function createLink(val) {
document.getElementById("link").innerHTML = val;
document.getElementById('buttonId').style.display = 'none';
document.getElementById('sentenceId').style.display = 'none';
localStorage.setItem("textArea", val);
}
function getText(){
var textVal = localStorage.getItem("textArea");
}
function navigateTo(val){
window.href.location = "b.html?text=" + val;
}
function getText(){
var url = window.location.href;
var queryIndex = url.indexOf("=") + 1;
var passedText = url.substring(queryIndex);
document.getElementById('foo').value = passedText;
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="UTF-8" />
<title>My New Pen!</title>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<!-- Styles -->
<link rel="stylesheet" href="styles/index.processed.css">
</head>
<body>
<button id="clickme">To load the textarea content, click me!</button>
<div id="content">The data from the textarea will be shown here, afte you click on the button :)</div>
<!-- Scripts -->
<script src="scripts/index.js"></script>
</body>
</html>
<textarea id="textarea">
I am the content of the textarea inside the textarea.html file.
</textarea>
b.html
function createLink(val) {
document.getElementById("link").innerHTML = val;
document.getElementById('buttonId').style.display = 'none';
document.getElementById('sentenceId').style.display = 'none';
localStorage.setItem("textArea", val);
}
function getText(){
var textVal = localStorage.getItem("textArea");
}
function navigateTo(val){
window.href.location = "b.html?text=" + val;
}
function getText(){
var url = window.location.href;
var queryIndex = url.indexOf("=") + 1;
var passedText = url.substring(queryIndex);
document.getElementById('foo').value = passedText;
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="UTF-8" />
<title>My New Pen!</title>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<!-- Styles -->
<link rel="stylesheet" href="styles/index.processed.css">
</head>
<body>
<button id="clickme">To load the textarea content, click me!</button>
<div id="content">The data from the textarea will be shown here, afte you click on the button :)</div>
<!-- Scripts -->
<script src="scripts/index.js"></script>
</body>
</html>
<textarea id="textarea">
I am the content of the textarea inside the textarea.html file.
</textarea>
另一种选择是使用查询字符串。 a.html
function createLink(val) {
document.getElementById("link").innerHTML = val;
document.getElementById('buttonId').style.display = 'none';
document.getElementById('sentenceId').style.display = 'none';
localStorage.setItem("textArea", val);
}
function getText(){
var textVal = localStorage.getItem("textArea");
}
function navigateTo(val){
window.href.location = "b.html?text=" + val;
}
function getText(){
var url = window.location.href;
var queryIndex = url.indexOf("=") + 1;
var passedText = url.substring(queryIndex);
document.getElementById('foo').value = passedText;
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="UTF-8" />
<title>My New Pen!</title>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<!-- Styles -->
<link rel="stylesheet" href="styles/index.processed.css">
</head>
<body>
<button id="clickme">To load the textarea content, click me!</button>
<div id="content">The data from the textarea will be shown here, afte you click on the button :)</div>
<!-- Scripts -->
<script src="scripts/index.js"></script>
</body>
</html>
<textarea id="textarea">
I am the content of the textarea inside the textarea.html file.
</textarea>
这将在导航期间通过url传递textarea
中的文本值。加载b.html
后,可以执行以下操作
b.html
function createLink(val) {
document.getElementById("link").innerHTML = val;
document.getElementById('buttonId').style.display = 'none';
document.getElementById('sentenceId').style.display = 'none';
localStorage.setItem("textArea", val);
}
function getText(){
var textVal = localStorage.getItem("textArea");
}
function navigateTo(val){
window.href.location = "b.html?text=" + val;
}
function getText(){
var url = window.location.href;
var queryIndex = url.indexOf("=") + 1;
var passedText = url.substring(queryIndex);
document.getElementById('foo').value = passedText;
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="UTF-8" />
<title>My New Pen!</title>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<!-- Styles -->
<link rel="stylesheet" href="styles/index.processed.css">
</head>
<body>
<button id="clickme">To load the textarea content, click me!</button>
<div id="content">The data from the textarea will be shown here, afte you click on the button :)</div>
<!-- Scripts -->
<script src="scripts/index.js"></script>
</body>
</html>
<textarea id="textarea">
I am the content of the textarea inside the textarea.html file.
</textarea>
如果您想打开一个新页面并在那里获取文本,可以使用post表单和输入[type=“hidden”]发送文本,然后再显示 如果要发送链接,则必须将文本编码为get参数,或者将其保存到数据库并将条目id添加到链接中
正如@Kramb已经提到的,本地存储是一种可能性,但前提是您保持在同一个浏览器上,并且两个页面具有相同的域。这可以使用
JavaScript
实现。您可以对网站上的另一个页面执行AJAX
调用,并搜索元素以获取其内容。在你的例子中,一个textarea
我给你写了一个例子。单击
为了使事情变得更简单,我在本例中使用了jQuery
那么它是如何工作的呢?
首先,将jQuery
包含在您网站的
标签中
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
上面的代码过滤掉#textarea
div,然后使用jQuery
html()
函数获取innerHTML
如果要通过[value]
属性获取textarea的值,可以将上面的行替换为
$(response).filter("#textarea").val();
我相信你想这样做:
函数createLink(){
var textvalue=document.getElementById('sentenceId').value;
document.getElementById(“link”).innerHTML=textvalue;
document.getElementById(“buttonId”).className=“hideme”;
document.getElementById(“sentenceId”).className=“hideme”;
}
.hideme{
显示:无;
}
提交
您想在b.html上显示textarea中的文本吗?我不太确定您想在这里实现什么,所以您需要创建指向新页面的可变链接?如果不是太多,请尝试使用本地存储。函数localStorage.setItem()
和localStorage.getItem()
将对您有所帮助。看@kramb是的,这就是我想要做的