在A网页里点击“打开”按钮后,利用JS打开网页B; 网页B里包含若干个文本框和一个“确定”按钮,用户点击“确定”按钮后,网页B检查是否有文本框输入为空; 若有空,将焦点定为到空文本框,否则关闭网页B,并将网页B中文本框内容显示到网页A中。
时间: 2023-06-25 07:02:31 浏览: 27
好的,这个需求可以通过以下步骤实现:
在网页A中编写JS代码,在“打开”按钮上添加点击事件,通过window.open()方法打开网页B:
```javascript
document.getElementById("openBtn").addEventListener("click", function() {
window.open("网页B的URL");
});
```
在网页B中编写JS代码,为“确定”按钮添加点击事件,检查文本框是否为空,若为空则将焦点定为到空文本框,否则将文本框内容传递给网页A并关闭网页B:
```javascript
document.getElementById("confirmBtn").addEventListener("click", function() {
var inputs = document.getElementsByTagName("input");
var isEmpty = false;
var emptyInput;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].value === "") {
isEmpty = true;
emptyInput = inputs[i];
break;
}
}
if (isEmpty) {
emptyInput.focus();
} else {
var text = "";
for (var i = 0; i < inputs.length; i++) {
text += inputs[i].value + " ";
}
window.opener.document.getElementById("textBox").value = text;
window.close();
}
});
```
在网页A中添加一个文本框和一个按钮,当网页B关闭后,将文本框传递的内容显示到网页A中:
```html
<input type="text" id="textBox">
<button id="showBtn">显示</button>
```
```javascript
document.getElementById("showBtn").addEventListener("click", function() {
var text = document.getElementById("textBox").value;
alert(text);
});
```
注意:在网页A中,需要将“打开”按钮和文本框的id属性和JS代码中的对应id值保持一致;在网页B中,需要将“确定”按钮和所有文本框的id属性和JS代码中的对应id值保持一致。