简体   繁体   English

如何在单击时将新的 UI 元素添加到列表中?

[英]How can I add new UI Elements to a list on click?

I need to create the following panel called "Categories" that each time the user clicks on the "+New Category" link,我需要创建以下名为“Categories”的面板,每次用户点击“+New Category”链接时,
A new Text Box rectangle is added on which the user can input the category.添加了一个新的文本框矩形,用户可以在其中输入类别。 For each added category an ordered number is signified on the side.对于每个添加的类别,旁边都标有一个有序编号。

Currently this is my code:目前这是我的代码:

HTML: HTML:

<div class="row">
     <div class="col-lg-3">
           <section class="panel">
                <header class="panel-heading">
                   <h2 class="panel-title">Categories</h2>
                   <a class="new-category" href="#add-new-app-category">+ New Category</a>
                </header>
           </section>
     </div>
</div> <!-- END <div class="row"> -->

CSS: CSS:

.panel {
    background: transparent;
    -webkit-box-shadow: none;
    box-shadow: none;
    border: none;
}

.panel {
    margin-bottom: 20px;
    background-color: #fff;
    border: 1px solid transparent;
/*    border-radius: 4px;*/
    -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
    box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
/*    overflow-y: auto;*/
    width:431px;
}


.panel-heading {
    background: #fdfdfd;
/*    border-radius: 5px 5px 0 0;*/
/*    border-bottom: 1px solid #DADADA;*/
/*    padding: 18px;*/
    position: relative;
}

.panel-title {
    margin-top: 0;
    margin-bottom: 0;
    font-size: 22px;
    color: #2baab1;
    font-family:'Lato', sans-serif;
    display:inline-block;
}

.panel-body {
    background: #fdfdfd;
    -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
/*    border-radius: 5px;*/
}


.panel-body {
    padding: 15px;
}


.new-category {
    float:right;
    margin-top:5px;
    text-decoration:none;
}

.new-category:link , .new-category:visited, .new-category:hover{
    text-decoration:none;
}

The jsfiddle link: https://jsfiddle.net/kzc024vq/2/ jsfiddle 链接: https ://jsfiddle.net/kzc024vq/2/

Each time the user clicks on the "+New Category" link, new ordered textbox UI elements should be added, like shown in this image:每次用户单击“+新类别”链接时,都应添加新的有序文本框 UI 元素,如下图所示:

在此处输入图片说明

In addition what was also required from me:此外,我还需要什么:

  1. I need to add the possibility to delete a category in the list by clicking on the "x" of that specific text box我需要通过单击该特定文本框的“x”来添加删除列表中类别的可能性

  2. That I can click and drag it over the other elements of the list.我可以单击并将其拖到列表的其他元素上。

You must use .append() to add new elements into html container, and .remove() to remove an specific element.您必须使用.append()将新元素添加到 html 容器中,并使用.remove()删除特定元素。 You could use click event to use add and remove elements.您可以使用 click 事件来使用添加和删除元素。

Example:例子:

 $( ".new-category" ).click(function() { $( '#container' ).append('<div>new category <a class="close-category" href="#">x</a></div>'); }); $( "#container" ).on( "click", ".close-category", function( event ) { $( this).parent().remove(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row" id="container"> <div class="col-lg-3"> <section class="panel"> <header class="panel-heading"> <h2 class="panel-title">Categories</h2> <a class="new-category" href="#add-new-app-category">+ New Category</a> </header> </section> </div> </div> <!-- END <div class="row"> -->

You need use delegate event to update events when you append new category because The new objects don't inherit the event.当您追加新类别时,您需要使用委托事件来更新事件,因为新对象不继承该事件。

$( "#container" ).on( "click", ".close-category", function( event ) {
        $( this).parent().remove();
    });
function createCategory(){
  var htmlStr = '<h2 class="panel-title">Categories</h2>
               <a class="new-category"href="#add-new-app-category">+ New Category</a>'
  $('#yourWrapperDiv').append(htmlStr);

}

and HTML side:和 HTML 方面:

<a id="newCategory" onclick="createCategory()">new category</a>

http://www.w3schools.com/jquery/html_append.asp http://www.w3schools.com/jquery/html_append.asp

if you want to give the new categories own names, you either need to add an input field to type the name in, or you need to generate unique IDs.如果您想为新类别赋予自己的名称,您需要添加一个输入字段来输入名称,或者您需要生成唯一的 ID。

if you dont want the click handler to be written html side you also can do:如果您不希望将点击处理程序写入 html 端,您也可以这样做:

$('#newCategoryLink').click(function(){
  createCategory();
});

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

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