<!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 =&gt; $url) {
			$locked = strpos($url, 'locked:') === 0;
			$url = $locked ? substr($url, 7) : $url;
			echo '&lt;tr&gt;';
			echo '&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;url-checkbox&quot; value=&quot;' . $index . '&quot; ' . ($locked ? 'disabled' : '') . '&gt;&lt;/td&gt;';
			echo '&lt;td&gt;&lt;a href=&quot;' . $url . '&quot; target=&quot;_blank&quot;&gt;' . $url . '&lt;/a&gt;&lt;/td&gt;';
			echo '&lt;td&gt;' . ($locked ? '锁定' : '正常') . '&lt;/td&gt;';
			echo '&lt;/tr&gt;';
		}
		?&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;div id=&quot;add-dialog&quot; class=&quot;dialog&quot;&gt;
	&lt;h2&gt;添加网址&lt;/h2&gt;
	&lt;form&gt;
		&lt;label&gt;网址:&lt;/label&gt;
		&lt;input type=&quot;text&quot; name=&quot;url-input&quot; required&gt;
		&lt;button type=&quot;submit&quot;&gt;添加&lt;/button&gt;
		&lt;button type=&quot;button&quot; class=&quot;close-btn&quot;&gt;取消&lt;/button&gt;
	&lt;/form&gt;
&lt;/div&gt;
&lt;div id=&quot;delete-dialog&quot; class=&quot;dialog&quot;&gt;
	&lt;h2&gt;删除网址&lt;/h2&gt;
	&lt;form&gt;
		&lt;p&gt;确认删除选中的网址吗?&lt;/p&gt;
		&lt;button type=&quot;submit&quot;&gt;删除&lt;/button&gt;
		&lt;button type=&quot;button&quot; class=&quot;close-btn&quot;&gt;取消&lt;/button&gt;
	&lt;/form&gt;
&lt;/div&gt;
&lt;div id=&quot;lock-dialog&quot; class=&quot;dialog&quot;&gt;
	&lt;h2&gt;锁定网址&lt;/h2&gt;
	&lt;form&gt;
		&lt;p&gt;确认锁定选中的网址吗?&lt;/p&gt;
		&lt;button type=&quot;submit&quot;&gt;锁定&lt;/button&gt;
		&lt;button type=&quot;button&quot; class=&quot;close-btn&quot;&gt;取消&lt;/button&gt;
	&lt;/form&gt;
&lt;/div&gt;
&lt;div id=&quot;unlock-dialog&quot; class=&quot;dialog&quot;&gt;
	&lt;h2&gt;解锁网址&lt;/h2&gt;
	&lt;form&gt;
		&lt;p&gt;确认解锁选中的网址吗?&lt;/p&gt;
		&lt;button type=&quot;submit&quot;&gt;解锁&lt;/button&gt;
		&lt;button type=&quot;button&quot; class=&quot;close-btn&quot;&gt;取消&lt;/button&gt;
	&lt;/form&gt;
&lt;/div&gt;
</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=&quot;url-checkbox&quot;]:checked');
		if (checked.length &gt; 0) {
			$('#delete-dialog').show();
		}
	});

	// 监听锁定按钮点击事件
	$('#lock-btn').click(function() {
		var checked = $('input[name=&quot;url-checkbox&quot;]:checked');
		if (checked.length &gt; 0) {
			$('#lock-dialog').show();
		}
	});

	// 监听解锁按钮点击事件
	$('#unlock-btn').click(function() {
		var checked = $('input[name=&quot;url-checkbox&quot;]:checked');
		if (checked.length &gt; 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=&quot;url-checkbox&quot;]: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=&quot;url-input&quot;]').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 &gt; 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">&lt;?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']) &amp;&amp; $_POST['action'] === 'add' &amp;&amp; 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(&quot;
&quot;, $urls));
			echo 'success';
		} else {
			echo 'invalid url';
		}
	 } else {
		echo 'url already exists';
	 }
 }

 // 删除网址
 if (isset($_POST['action']) &amp;&amp; $_POST['action'] === 'delete' &amp;&amp; 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(&quot;
&quot;, $urls));
	 echo 'success';
 }

 // 锁定网址
 if (isset($_POST['action']) &amp;&amp; $_POST['action'] === 'lock' &amp;&amp; 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(&quot;
&quot;, $urls));
	 echo 'success';
 }

 // 解锁网址
 if (isset($_POST['action']) &amp;&amp; $_POST['action'] === 'unlock' &amp;&amp; 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(&quot;
&quot;, $urls));
	 echo 'success';
 }
?&gt;
</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=&quot;text&quot;] {
	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=&quot;submit&quot;] {
	margin-right: 10px;
}

.dialog button[type=&quot;button&quot;] {
	background-color: #ccc;
}

.dialog p {
	margin-bottom: 0;
}
</code></pre>
网址收藏夹:完整代码实现与优化

原文地址: https://www.cveoy.top/t/topic/mPkY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录