jQuery 收藏代码通常是指使用 jQuery 库来实现网页上的收藏功能。这个功能允许用户将当前页面添加到他们的浏览器书签中,以便以后快速访问。
以下是一个简单的 jQuery 收藏代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery 收藏示例</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="favorite-btn">收藏本页</button>
<script>
$(document).ready(function() {
$('#favorite-btn').click(function() {
if (window.sidebar && window.sidebar.addPanel) { // Firefox
window.sidebar.addPanel(document.title, window.location.href, "");
} else if (window.external && ('AddFavorite' in window.external)) { // Internet Explorer
window.external.AddFavorite(location.href, document.title);
} else if (window.opera && window.print) { // Opera
return true;
} else { // Other browsers (Chrome, Safari)
alert('请按 Ctrl+D 或 Cmd+D 手动收藏本页。');
}
});
});
</script>
</body>
</html>通过上述方法,可以实现一个兼容性较好的 jQuery 收藏功能。