简体   繁体   English

将 div 加入书签,然后将其加载到单独的 html 文件中

[英]Bookmark a div, then load it in a separate html file

I have a page with multiple divs.我有一个包含多个 div 的页面。 Users can bookmark each div by clicking an icon.用户可以通过单击图标为每个 div 添加书签。 When the icon is clicked, I'm adding a .saved-item class to the div.单击图标时,我将.saved-item class 添加到 div。 I have a separate page where I'd like to show all bookmarked divs but I can't figure out how to do it properly.我有一个单独的页面,我想在其中显示所有带书签的 div,但我不知道如何正确地做到这一点。

Function which adds class to the div: Function 将 class 添加到 div:

$('.bookmark').on('click', function(){
    $(this).parent().parent().addClass('saved-item');
});

In another file, I then try to load all saved items:在另一个文件中,然后我尝试加载所有保存的项目:

$("#content").load( "speaking-mistakes.html #wrapper .saved-item" );

Unfortunately, it's not working as the addClass function is not permanent.不幸的是,它不起作用,因为 addClass function 不是永久性的。 I also tried adding the div to the localStorage, but it doesn't save the entire div layout and its content (with buttons, text, style, etc.) and I don't think that loading up localStorage with possibly hundreds of divs would be a good idea.我还尝试将 div 添加到 localStorage,但它不会保存整个 div 布局及其内容(带有按钮、文本、样式等),而且我认为加载 localStorage 可能有数百个 div 不会是个好主意。

Is there any possibility to dynamically "move" or "export" a div from one HTML file to another HTML file?是否有可能将 div 从一个 HTML 文件动态“移动”或“导出”到另一个 HTML 文件?

Thank you for your help.谢谢您的帮助。

It depends where the content is coming from.这取决于内容的来源。

These things are normally saved in a database server side.这些东西通常保存在数据库服务器端。 And when an icon is clicked, you can add an entry in the bookmarks table in your database.当单击图标时,您可以在数据库的书签表中添加一个条目。

If you want to do it through client side, you can use indexedDb or local storage如果你想通过客户端来做,你可以使用 indexedDb 或本地存储

So you would have an array of bookmarked ids所以你会有一个带书签的 id 数组

const ids = [ 1, 35, 64 ]

when you click an icon it can push the id to this array which is then saved into localstorage like this当您单击图标时,它可以将 id 推送到该数组,然后像这样保存到本地存储中

localStorage.setItem("bookmarks", JSON.stringify(ids))

As an extra, if a user is signed in, you can add their user id as item 0 in the bookmarks array as a way to 'authenticate' them so when other users log in, they dont see the previous users bookmarks.另外,如果用户已登录,您可以将他们的用户 ID 添加为书签数组中的第 0 项,作为“验证”他们的一种方式,这样当其他用户登录时,他们就不会看到以前的用户书签。

This should really be done server side for best practise这真的应该在服务器端完成以获得最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM