js 修改alert的标题
时间: 2023-09-29 20:09:53 浏览: 96
`alert` 方法是浏览器自带的方法,无法修改其标题。不过,你可以使用第三方 JavaScript 库或框架来创建自定义的警告框,以实现修改标题的效果。
例如,使用 jQuery UI 的 `dialog` 方法,可以自定义警告框的标题。示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Custom Alert Dialog</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<script>
$(function() {
$("#dialog").dialog({
autoOpen: false,
title: "Custom Alert",
modal: true,
buttons: {
OK: function() {
$(this).dialog("close");
}
}
});
$("#show-dialog").click(function() {
$("#dialog").dialog("open");
});
});
</script>
<button id="show-dialog">Show Custom Alert</button>
<div id="dialog">
<p>This is a custom alert dialog.</p>
<p>You can modify the title, content and buttons.</p>
</div>
</body>
</html>
```
在这个示例中,我们使用 jQuery UI 的 `dialog` 方法来创建一个自定义的警告框,并设置了标题为“Custom Alert”。点击“Show Custom Alert”按钮即可打开这个警告框。