网址收藏夹:完整代码实现与优化
<!DOCTYPE html>
<html>
<head>
<title>网址收藏夹</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<h1>网址收藏夹</h1>
<div class="toolbar">
<button id="add-btn">添加</button>
<button id="delete-btn">删除</button>
<button id="lock-btn">锁定</button>
<button id="unlock-btn">解锁</button>
<input type="text" id="search-input" placeholder="搜索">
</div>
<table id="url-table">
<thead>
<tr>
<th></th>
<th>网址</th>
<th>状态</th>
</tr>
</thead>
<tbody>
<?php
// 读取已保存的网址
$url_file = 'urls.txt';
if (file_exists($url_file)) {
$urls = file($url_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
} else {
$urls = array();
}
<pre><code> // 输出网址表格
foreach ($urls as $index => $url) {
$locked = strpos($url, 'locked:') === 0;
$url = $locked ? substr($url, 7) : $url;
echo '<tr>';
echo '<td><input type="checkbox" name="url-checkbox" value="' . $index . '" ' . ($locked ? 'disabled' : '') . '></td>';
echo '<td><a href="' . $url . '" target="_blank">' . $url . '</a></td>';
echo '<td>' . ($locked ? '锁定' : '正常') . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<div id="add-dialog" class="dialog">
<h2>添加网址</h2>
<form>
<label>网址:</label>
<input type="text" name="url-input" required>
<button type="submit">添加</button>
<button type="button" class="close-btn">取消</button>
</form>
</div>
<div id="delete-dialog" class="dialog">
<h2>删除网址</h2>
<form>
<p>确认删除选中的网址吗?</p>
<button type="submit">删除</button>
<button type="button" class="close-btn">取消</button>
</form>
</div>
<div id="lock-dialog" class="dialog">
<h2>锁定网址</h2>
<form>
<p>确认锁定选中的网址吗?</p>
<button type="submit">锁定</button>
<button type="button" class="close-btn">取消</button>
</form>
</div>
<div id="unlock-dialog" class="dialog">
<h2>解锁网址</h2>
<form>
<p>确认解锁选中的网址吗?</p>
<button type="submit">解锁</button>
<button type="button" class="close-btn">取消</button>
</form>
</div>
</code></pre>
</body>
</html>
```
<pre><code class="language-javascript">$(document).ready(function() {
// 监听添加按钮点击事件
$('#add-btn').click(function() {
$('#add-dialog').show();
});
// 监听删除按钮点击事件
$('#delete-btn').click(function() {
var checked = $('input[name="url-checkbox"]:checked');
if (checked.length > 0) {
$('#delete-dialog').show();
}
});
// 监听锁定按钮点击事件
$('#lock-btn').click(function() {
var checked = $('input[name="url-checkbox"]:checked');
if (checked.length > 0) {
$('#lock-dialog').show();
}
});
// 监听解锁按钮点击事件
$('#unlock-btn').click(function() {
var checked = $('input[name="url-checkbox"]:checked');
if (checked.length > 0) {
$('#unlock-dialog').show();
}
});
// 监听搜索框输入事件
$('#search-input').on('input', function() {
var keyword = $(this).val().trim();
if (keyword !== '') {
$('#url-table tbody tr').hide();
$('#url-table tbody tr:contains('' + keyword + '')').show();
} else {
$('#url-table tbody tr').show();
}
});
// 监听对话框提交事件
$('.dialog form').submit(function(e) {
e.preventDefault();
var dialogId = $(this).parents('.dialog').attr('id');
var action = '';
var checked = $('input[name="url-checkbox"]:checked');
switch (dialogId) {
case 'add-dialog':
action = 'add';
break;
case 'delete-dialog':
action = 'delete';
break;
case 'lock-dialog':
action = 'lock';
break;
case 'unlock-dialog':
action = 'unlock';
break;
}
if (action === 'add') {
var newUrl = $(this).find('input[name="url-input"]').val().trim();
if (newUrl !== '') {
$.post('action.php', {action: action, url: newUrl}, function(data) {
if (data === 'success') {
location.reload();
} else {
alert('添加网址失败,请重试');
}
});
} else {
alert('网址不能为空');
}
} else if (action === 'delete' || action === 'lock' || action === 'unlock') {
if (checked.length > 0) {
var selected = checked.map(function() {
return $(this).val();
}).get();
$.post('action.php', {action: action, urls: selected}, function(data) {
if (data === 'success') {
location.reload();
} else {
alert('操作失败,请重试');
}
});
} else {
alert('请选择要操作的网址');
}
}
});
// 监听对话框关闭按钮点击事件
$('.dialog .close-btn').click(function() {
$(this).parents('.dialog').hide();
});
});
</code></pre>
<pre><code class="language-php"><?php
// 读取已保存的网址
$url_file = 'urls.txt';
if (file_exists($url_file)) {
$urls = file($url_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
} else {
$urls = array();
}
// 添加新网址
if (isset($_POST['action']) && $_POST['action'] === 'add' && isset($_POST['url'])) {
$newUrl = $_POST['url'];
if (!in_array($newUrl, $urls)) {
if (filter_var($newUrl, FILTER_VALIDATE_URL)) {
$urls[] = $newUrl;
file_put_contents($url_file, implode("
", $urls));
echo 'success';
} else {
echo 'invalid url';
}
} else {
echo 'url already exists';
}
}
// 删除网址
if (isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['urls'])) {
$selected = $_POST['urls'];
foreach ($selected as $index) {
if (array_key_exists($index, $urls)) {
unset($urls[$index]);
}
}
file_put_contents($url_file, implode("
", $urls));
echo 'success';
}
// 锁定网址
if (isset($_POST['action']) && $_POST['action'] === 'lock' && isset($_POST['urls'])) {
$selected = $_POST['urls'];
foreach ($selected as $index) {
if (array_key_exists($index, $urls)) {
$urls[$index] = 'locked:' . $urls[$index];
}
}
file_put_contents($url_file, implode("
", $urls));
echo 'success';
}
// 解锁网址
if (isset($_POST['action']) && $_POST['action'] === 'unlock' && isset($_POST['urls'])) {
$selected = $_POST['urls'];
foreach ($selected as $index) {
if (array_key_exists($index, $urls)) {
$urls[$index] = str_replace('locked:', '', $urls[$index]);
}
}
file_put_contents($url_file, implode("
", $urls));
echo 'success';
}
?>
</code></pre>
<pre><code class="language-css">body {
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
}
.toolbar {
margin-bottom: 20px;
}
#url-table {
border-collapse: collapse;
width: 100%;
}
#url-table th, #url-table td {
border: 1px solid #ccc;
padding: 8px;
}
#url-table th {
background-color: #eee;
}
#url-table tbody tr:hover {
background-color: #f0f0f0;
}
#url-table td:first-child {
width: 20px;
}
#url-table td:last-child {
width: 80px;
text-align: center;
}
#search-input {
margin-left: 20px;
padding: 6px;
border: 1px solid #ccc;
border-radius: 4px;
}
.dialog {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
z-index: 999;
}
.dialog h2 {
margin-top: 0;
}
.dialog label {
display: inline-block;
width: 60px;
}
.dialog input[type="text"] {
width: 200px;
padding: 6px;
border: 1px solid #ccc;
border-radius: 4px;
}
.dialog button {
margin-top: 10px;
padding: 6px 12px;
background-color: #007bff;
border: none;
border-radius: 4px;
color: #fff;
cursor: pointer;
}
.dialog button[type="submit"] {
margin-right: 10px;
}
.dialog button[type="button"] {
background-color: #ccc;
}
.dialog p {
margin-bottom: 0;
}
</code></pre>
原文地址: https://www.cveoy.top/t/topic/mPkY 著作权归作者所有。请勿转载和采集!