javascript - 如何从用户添加 div 和内容?

标签 javascript jquery html css

我实际上是网络领域的初学者。我是学习者,也在做这个元素。我在制作一个自行创建的 div 时遇到问题,其中包含链接等内容和一些用户定义的文本。每当用户按下 ADD 按钮时,div 将自动创建,并要求用户添加标题(成为链接)等内容,然后添加一些文本。
你能告诉我我应该使用哪些 JavaScript 或 jquery 函数吗?


喜欢:

                                                          ---------
                                                         |   ADD   |
                        -----------  //First div          ---------
                       |  Link     |
                       |           |
                       |  Text     |
                       |description|
                       |           |
                        -----------


                        ----------- //Second div
                       |  Link     |
                       |           |
                       |  Text     |
                       |description|
                       |           |
                        -----------

                        ----------- //Third Div
                       |  Link     |
                       |           |
                       |  Text     |
                       |description|
                       |           |
                        -----------


最佳答案

你可以做一个简单的脚本,以这种方式使用两个段落标签附加链接和文本描述:

$(function () {
  $("#addBtn").click(function () {
    $("#target").append('<li><p><a href="#">Link Description<\/a><\/p><p>Text Description<\/p><\/li>');
  });
});
/* Global Reset */
* {font-family: Segoe UI; margin: 0; padding: 0; list-style: none;}
a {text-decoration: none;}

/* Margin for Para */
p {margin: 0 0 5px;}
/* Box for the text content */
ul {display: block; margin: auto; width: 50%;}
li {border: 1px solid #999;}

/* Style for Button */
#addBtn {display: block; padding: 3px 10px; cursor: pointer; margin: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- Add Button -->
<input type="button" value="Add" id="addBtn" />

<!-- We will use an unordered list for adding the contents and use an ID target -->
<ul id="target"></ul>

如果需要,您可以像下面那样添加 contenteditable 以使其可编辑。

$(function () {
  $("#addBtn").click(function () {
    $("#target").append('<li><p contenteditable><a href="#">Link Description (Click and Edit)<\/a><\/p><p contenteditable>Text Description (Click and Edit)<\/p><\/li>');
  });
});
/* Global Reset */
* {font-family: Segoe UI; margin: 0; padding: 0; list-style: none;}
a {text-decoration: none;}

/* Margin for Para */
p {margin: 0 0 5px;}
/* Box for the text content */
ul {display: block; margin: auto; width: 50%;}
li {border: 1px solid #999;}

/* Style for Button */
#addBtn {display: block; padding: 3px 10px; cursor: pointer; margin: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- Add Button -->
<input type="button" value="Add" id="addBtn" />

<!-- We will use an unordered list for adding the contents and use an ID target -->
<ul id="target"></ul>

关于javascript - 如何从用户添加 div 和内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158148/

相关文章:

javascript - 使用 django/javascript 保存 DnD 状态

javascript - 为什么 not($(this)) 之后的回调函数会重复多次?

javascript - 图像上的 contenteditable removeFormat

javascript - 如何编写一个函数来检查输入是否实际上是整数? (Javascript)

javascript - typescript 返回类型为 'void' 的参数不能分配给类型为“TimerHandler?”的参数?

javascript - 如何一次验证所有输入字段?

javascript - Jquery mobile $.mobile.changePage 未加载脚本

javascript - 进行测验并需要根据 Javascript 中的正确选择更改背景颜色

jQuery 更改事件在 IE11 中不起作用

html - 在谷歌浏览器中隐藏视频的 native 播放按钮