当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to make an interactive website and allow the user to edit website content?

问题描述:

To begin with, I'm fairly knowledgeable with both html5 and css3. Right now I'm trying to create a website that allows visitors to edit website content so that all visitors are able to see the edits. I know about contenteditable but this attribute does not last when the page is refreshed and no other visitors to see the edits. For example:

<p> Edit me please </p>

A visitor would be able to edit the element above by visiting the website and simply selecting and typing. Also I have heard that javascript, jquery, sql, and php can all allow visitors to edit website content but I don't know which is the best.

To sum up, if anyone knows how to allow visitors of a website to edit its' content it would be much appreciated to share their knowledge.

网友答案:

Create a database table (lets call it "strings") , and just define 2 columns - "string_identifier" and "string_value"

When you render the page, simply query the database to fetch the strings and their identifiers. Each editable element should have an attribute containing its identifier.

You can use jquery to capture the click event on all editable elements, and replace it with an input with that text.

Then assign an event handler to that input, so when the enter key is pressed, it sends a request to a php script (with the identifier and new string as data) on the server to update the database with the new string.

If you would like live updates to all other clients (no need to refresh), use jquery to continuously request a file on the server (endless loop), which will return a JSON object containing all the strings. Then go through and replace the text of all editable elements on the data with the new strings you have received from the server.

分享给朋友:
您可能感兴趣的文章:
随机阅读: