如何实现这个删除功能

时间:2015-04-28 00:19:58

标签: javascript jquery

我正在尝试创建一个删除按钮,删除按钮所在的li标签之间的所有内容。

$(document).ready(function() {

    $('#Adicionar1').click(function() {
        $('#list1').append("<li>"+ $("#Texto1").val() +"<button>remover</button>" +"</li>");
$("#Texto1").val("");        
    });

    $('button').click(function() {


    });


});

代码: http://jsfiddle.net/bdMAF/917/

3 个答案:

答案 0 :(得分:2)

您无法将事件添加到不存在的元素。您需要在添加按钮时附加事件或使用事件委派。

// Header Files
#include <cstdlib>
#include <curses.h>
#include <iostream>
#include <windows.h>


using namespace std; 

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()


// Function Prototypes

void titleScreen(); // Prints Title and instructions


void mapCreation( char arr[][12], int level);

void drawMap(char arr[][12]);

bool update(char arr[][12], int &level, int &lives, int &score);


// Main Program
int main ()
    {
    // initialize variables
    int option;
    char baseMap[12][12];
    int level = 1;
    int lives = 3;
    int score = 0;
    bool gameOver = false;
    bool levelCompleted = false;


    SetConsoleTextAttribute(console, 240); // change background to white

    system("CLS");// clears screen in order to remove black background

    titleScreen(); // Display Title

    do // do-while loop starts
    {

    cin >> option; // take in input

        if(option == 1) // temporary option to check for next screen
            {
            //Display Maze

            system("CLS");// clears screen in order to remove black background
            while(gameOver == false)
                {
                 mapCreation( baseMap, level );
                 while(gameOver == false || levelCompleted == false )
                      {
                       drawMap(baseMap);
                       update(baseMap, level, lives, score);
                      }
                }
             }
        }
    while( option !=1); // condition of do-while loop

    system("pause"); // Pause for user, only temporary


    return 0;

   }








void titleScreen(){
    cout << " Welcome to Treasure Hunter!\n\n";
    cout << "In order to beat this game you must find the treasure\n";
    cout << " that is located in the maze. You can move using the \n";
    cout << " arrow keys or WASD.\n\n";
    cout << " Warning! There are traps that will take life away as\n";
    cout << " well as add life! However, they are hidden so be careful!\n ";
    cout << " Goodluck and have fun!\n\n\n\n";


}


void mapCreation( char arr[][12], int level )
    {
      int traps = 0;   
      int lives = 0;
      int treasure = 0;
      int x;
      int y;
        for(int i = 0; i < 12; i++)
            {
             for(int j = 0; j < 12; j++)
                {
                 arr[i][j] = 0;
                }

            }
        arr[1][1] = '1';
        switch (level)
        {
        case 1:
        arr[0][1] = '|';
        arr[1][1] = '|';
        arr[2][1] = '|';
        arr[2][2] = '|';
        arr[2][3] = '|';
        arr[2][4] = '|';
        arr[0][6] = '|';
        arr[1][6] = '|';
        arr[1][8] = '|';
        arr[2][6] = '|';
        arr[2][8] = '|';
        arr[3][4] = '|';
        arr[3][6] = '|';
        arr[3][7] = '|';
        arr[3][8] = '|';
        arr[4][4] = '|';
        arr[4][6] = '|';
        arr[5][1] = '|';
        arr[6][1] = '|';
        arr[6][3] = '|';
        arr[6][4] = '|';
        arr[6][5] = '|';
        arr[6][6] = '|';
        arr[6][7] = '|';
        arr[7][1] = '|';
        arr[7][6] = '|';
        arr[8][1] = '|';
        arr[8][6] = '|';
        arr[8][8] = '|';
        arr[9][1] = '|';
        arr[9][6] = '|';
        arr[9][8] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 2)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 1)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 2: // Level 2 Map

        arr[0][9] = '\n';
        arr[1][0] = '|';
        arr[1][1] = '|';
        arr[1][2] = '|';
        arr[1][4] = '|';
      //  arr[1][0] = '\n';
        arr[2][2] = '|';
        arr[2][4] = '|';
        arr[2][5] = '|';
        arr[2][6] = '|';
        arr[2][7] = '|';
        arr[2][8] = '|';
      //  arr[2][9] = '\n';
        arr[3][2] = '|';
        arr[3][4] = '|';
        arr[3][7] = '|';
      //  arr[3][9] = '\n';
        arr[4][7] = '|';
        arr[4][9] = '\n';
        arr[5][2] = '|';
        arr[5][4] = '|';
        arr[5][7] = '|';
      //  arr[5][9] = '\n';
        arr[6][0] = '|';
        arr[6][1] = '|';
        arr[6][2] = '|';
        arr[6][4] = '|';
        arr[6][6] = '|';
        arr[6][7] = '|';
        arr[6][8] = '|';
      //  arr[6][9] = '\n';
        arr[7][3] = '|';
        arr[7][5] = '|';
        arr[7][9] = '\n';
        arr[8][4] = '|';
        arr[9][4] = '|';
     //   arr[9][9] = '\n';
        arr[0][11] = '|';
        arr[1][11] = '|';
        arr[2][11] = '|';
        arr[3][11] = '|';
        arr[4][11] = '|';
        arr[5][11] = '|';
        arr[6][11] = '|';
        arr[7][11] = '|';
        arr[8][11] = '|';
        arr[9][11] = '|';
        arr[10][11] = '|';
      //  arr[11][11] = '\n';
        arr[10][0] = '|';
        arr[10][1] = '|';
        arr[10][2] = '|';
        arr[10][3] = '|';
        arr[10][4] = '|';
        arr[10][5] = '|';
        arr[10][6] = '|';
        arr[10][7] = '|';
        arr[10][8] = '|';
        arr[10][9] = '|';
        arr[10][11] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 4)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 2)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 3: // Level 3 Map

        arr[1][4] = '|';
        arr[1][6] = '|';
        arr[1][7] = '|';
        arr[1][8] = '|';
        arr[1][9] = '|';
        arr[2][2] = '|';
        arr[2][4] = '|';
        arr[3][0] = '|';
        arr[3][1] = '|';
        arr[3][2] = '|';
        arr[3][3] = '|';
        arr[3][4] = '|';
        arr[3][6] = '|';
        arr[4][6] = '|';
        arr[5][3] = '|';
        arr[5][2] = '|';
        arr[5][6] = '|';
        arr[5][7] = '|';
        arr[5][8] = '|';
        arr[5][9] = '|';
        arr[6][0] = '|';
        arr[6][1] = '|';
        arr[6][2] = '|';
        arr[6][6] = '|';
        arr[7][4] = '|';
        arr[7][5] = '|';
        arr[7][6] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 6)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 3)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 4:
        arr[3][2] = '|';
        arr[3][3] = '|';
        arr[3][4] = '|';
        arr[3][5] = '|';
        arr[3][6] = '|';
        arr[3][7] = '|';
        arr[4][3] = '|';
        arr[5][3] = '|';
        arr[5][5] = '|';
        arr[5][6] = '|';
        arr[5][7] = '|';
        arr[5][8] = '|';
        arr[6][3] = '|';
        arr[6][5] = '|';
        arr[6][8] = '|';
        arr[7][3] = '|';
        arr[7][5] = '|';
        arr[7][8] = '|';
        arr[8][3] = '|';
        arr[8][5] = '|';
        arr[8][8] = '|';
        arr[9][3] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 8)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 4)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 5:
        arr[0][1] = '|';
        arr[1][1] = '|';
        arr[1][6] = '|';
        arr[2][3] = '|';
        arr[2][4] = '|';
        arr[2][5] = '|';
        arr[2][6] = '|';
        arr[3][1] = '|';
        arr[3][6] = '|';
        arr[4][1] = '|';
        arr[4][2] = '|';
        arr[4][3] = '|';
        arr[4][4] = '|';
        arr[4][5] = '|';
        arr[4][6] = '|';
        arr[4][7] = '|';
        arr[4][8] = '|';
        arr[4][9] = '|';
        arr[5][1] = '|';
        arr[7][2] = '|';
        arr[7][3] = '|';
        arr[7][4] = '|';
        arr[7][5] = '|';
        arr[7][6] = '|';
        arr[7][7] = '|';
        arr[7][8] = '|';
        arr[8][3] = '|';
        arr[8][6] = '|';
        arr[9][6] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 10)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 5)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }        
        break;
        }
    }

void drawMap(char arr[][12])
    {
     for(int i = 0; i < 12; i++)
        {
         for(int j = 0; j < 12; j++ )
            {
             if(arr[i][j] != 3 && arr[i][j] != 4)
               {
                cout << arr[i][j];
               }
            }
        }
    }

bool update(char arr[][12], int &level, int &lives, int &score)
    {
     bool levelCompleted = false;
     bool gameOver = false;

     return 0; // temporary holder
    }

答案 1 :(得分:1)

我会使用jQuery&#39; .parent()函数,并使用data属性:

&#13;
&#13;
$(document).ready(function() {

    $('.Adicionar').click(function() {
        console.log($(this).data("listid"));
        console.log($(this).data("textid"));
        
        $("#"+$(this).data("listid")).append("<li>"+         $("#"+$(this).data("textid")).val() +'<button class="remove">remover</button>' +"</li>");
        
    $("#"+$(this).data("textid")).val("");    
        $('.remove').on("click",function() {       
            $(this).parent().remove();
        });
    });



});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<h3>TEMAS A APRENDER</h3>
<input id= "Texto1" type="text" name="" value="">
    <button class="Adicionar" data-listid="list1" data-textid="Texto1">Adicionar</button>
<ul id="list1"></ul>

<h3>TEMAS APRENDIDOS</h3>
<input id= "Texto2" type="text" name="" value="">
    <button class="Adicionar" data-listid="list2" data-textid="Texto2">Adicionar</button>
<ul id="list2"></ul>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

上述类似答案:in this meetup talk: "Bridging Imperative APIs with Kinetophone"

HTML

<span class="delegateAnchor">
    <h3>TEMAS A APRENDER</h3>

    <input id="Texto1" type="text" name="" value="">
    <button id="Adicionar1">Adicionar</button>
    <ul id="list1"></ul>

    <h3>TEMAS APRENDIDOS</h3>

    <input id="Texto2" type="text" name="" value="">
    <button id="Adicionar2">Adicionar</button>
    <ul id="list2"></ul>
</span>

的Javascript

$(document).ready(function () {
    var $list1 = $('#list1');
    var $text1 = $("#Texto1");

    $('#Adicionar1').click(function () {
        var $li = $('<li>');
        $li.append($text1.val()).append("<button>remover</button>");
        $list1.append($li);
        $text1.val('');
    });

    $('.delegateAnchor > ul').on('click', 'button', function() {
        $(this).parent().remove();
    });
});