window.confirm方法中的确定怎么改
时间: 2024-03-03 16:48:53 浏览: 41
您可以使用 JavaScript 来更改 window.confirm 方法中的确定按钮的文本,但是这并不是一个好的做法,因为这是一个浏览器原生的方法,更改它可能导致意想不到的问题。以下是一个更改确定按钮文本的示例代码:
```
window.confirm = function(message) {
var iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.appendChild(iframe);
var response = iframe.contentWindow.confirm(message.replace("确定", "改变后的文本"));
document.body.removeChild(iframe);
return response;
};
```
但是,我建议您不要更改浏览器原生方法的行为,而是使用自己的对话框或模态窗口来代替 window.confirm。
相关问题
window.confirm方法中的确定按钮文本怎么改
更改 `window.confirm` 方法中的确定按钮文本并不是一个好的做法,因为这是浏览器原生的方法,更改它可能导致意想不到的问题。但是,您可以创建自定义对话框或使用第三方插件来代替 `window.confirm` 方法,这样您就可以完全控制按钮的文本了。
如果您一定要更改 `window.confirm` 方法中的确定按钮文本,可以使用以下代码:
```
window.confirm = function(message) {
var iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.appendChild(iframe);
var confirmBox = iframe.contentWindow.confirm(message);
var confirmBtn = iframe.contentWindow.document.querySelector("[value='确定']");
confirmBtn.value = "自定义文本";
document.body.removeChild(iframe);
return confirmBox;
};
```
上述代码中,我们创建了一个隐藏的 `iframe` 元素,并在其中调用原生的 `confirm` 方法来显示带有默认文本的确认框。然后我们获取确认框中的确定按钮,并将其文本更改为自定义文本,最后移除 `iframe` 元素并返回确认框的结果。
但是,请注意这种方法可能会有浏览器兼容性问题。而且,一般不建议修改原生方法的默认行为。如果您想要更好的控制对话框的外观和功能,最好是使用自定义的对话框或第三方插件。
window.confirm弹窗 修改按钮文字
可以使用以下代码来修改 window.confirm 弹窗的按钮文字:
```
// 保存原始的 confirm 方法
var originalConfirm = window.confirm;
// 重写 confirm 方法
window.confirm = function(message) {
// 创建一个新的 Promise 对象
return new Promise(function(resolve, reject) {
// 创建一个确认弹窗
var confirmPopup = document.createElement("div");
confirmPopup.classList.add("confirm-popup");
// 创建文本节点,并插入到确认弹窗中
var messageNode = document.createTextNode(message);
confirmPopup.appendChild(messageNode);
// 创建确认按钮,并插入到确认弹窗中
var confirmButton = document.createElement("button");
confirmButton.classList.add("confirm-button");
confirmButton.innerText = "我的确认按钮文字";
confirmPopup.appendChild(confirmButton);
// 创建取消按钮,并插入到确认弹窗中
var cancelButton = document.createElement("button");
cancelButton.classList.add("cancel-button");
cancelButton.innerText = "取消";
confirmPopup.appendChild(cancelButton);
// 将确认弹窗插入到文档中
document.body.appendChild(confirmPopup);
// 绑定确认按钮的点击事件处理程序
confirmButton.addEventListener("click", function() {
// 从文档中移除确认弹窗
document.body.removeChild(confirmPopup);
// 调用原始的 confirm 方法,并传入 true
resolve(true);
});
// 绑定取消按钮的点击事件处理程序
cancelButton.addEventListener("click", function() {
// 从文档中移除确认弹窗
document.body.removeChild(confirmPopup);
// 调用原始的 confirm 方法,并传入 false
resolve(false);
});
});
};
// 在代码中使用修改后的 confirm 方法
window.confirm("请确认你的选择。").then(function(result) {
if (result) {
console.log("你点击了确认按钮。");
} else {
console.log("你点击了取消按钮。");
}
});
// 恢复原始的 confirm 方法
window.confirm = originalConfirm;
```
在上面的代码中,我们首先保存了原始的 confirm 方法,然后重写了它。重写后的 confirm 方法返回一个 Promise 对象,通过创建一个自定义的确认弹窗来替代原始的确认弹窗。在自定义的确认弹窗中,我们可以根据需要修改确认按钮的文字。最后,我们在代码中使用修改后的 confirm 方法,并在结果中处理用户的选择。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<body>
<input type="button" value="新增" onclick="add()">
ID
姓名
年龄
密码
修改 删除
<c:if test="${not empty list }">
<c:forEach items="${list }" var="user" varStatus="s">
${s.count}
${user.username}
${user.age}
${user.password}
修改
删除
</c:forEach>
</c:if>
<script >function add(){
location.href="add.jsp";
}
function updatemethod(userid) {
var id = parseInt('<%= session.getAttribute("id") %>');
if (userid != id) {
alert("你不能修改别人的信息");
} else {
window.location.href = "update.jsp";
}
}
function deletemethod(userid) {
var id = parseInt('<%= session.getAttribute("id") %>');
if (userid != id) {
alert("你不能删除别人的信息");
} else {
if(confirm("你确定要删除自己的信息吗?")) {
window.location.href = "index.jsp";
}
}
}</script>
</body>
</html>上述代码哪里报错,要如何修改
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)