更改动态创建按钮的文本和颜色

时间:2018-02-16 00:11:48

标签: javascript

我有一个动态创建的红色按钮,并与一些用户输入一起放入li。看起来像这样

this

如果用户点击红色按钮,则li元素将被添加到另一个li

this

我希望能够将按钮从红色更改为绿色,并在文本移动到第二个li时将文本从“已启动”更改为“取消标记”,如果再次单击该按钮,则将文本更改为带有正确文本的红色把它搬回来

我不太确定在哪里编写函数来执行此操作?我应该在以前的书面函数之外还是在每个相关函数之外执行此操作?如果是这样,它会是什么样子,因为我的按钮只有一个id而没有值,这是我在stackoverflow上看到的方法。

谢谢!

编辑:代码段看起来它没有显示第二列(配置全部关闭)......它能在其他用户的笔记本电脑上运行吗?

// Dynamically creates a new li element with 'started' button after user puts in text and clicks a button similar to 'submit'
$(document).ready(
  $("#new-item").on('click', function() {
    // once the document loads, create new item with this function
    var text = $('#task').val();
    if (text != '') {
      $('#todo-list').prepend("<li class='addedTask'> <button id='started' value='on'>Started</button>" + text + '</li>');
    }

  })
);

// Clicking on the 'started' button will move the task from 'todo-list' to 'doing-list'
$("#todo-list").on('click', "button", function() {

  var completedItem = $(this).parent();
  $('#doing-list').prepend($(completedItem));
});

// Clicking on the 'unmark' button will moe the task back from 'doing-list' to 'todo-list'
$("#doing-list").on('click', "button", function() {
    var completedItem = $(this).parent();
    $('#todo-list').prepend($(completedItem));
});
header {
	background-color: #026aa7;
	height: 30px;
	text-align: center;
	padding: 5px 8px;
}

header a {
	height: 30px;
	width: 80px;
	text-align: center;
	background-image: url(https://a.trellocdn.com/dist/images/header-logo-2x.01ef898811a879595cea.png);
	background-repeat: no-repeat;
	text-align: center;
	display: inline-block;
	background-size: 80px 30px;
}

body {
	background-color: #0078c0;
	margin: 0px;
    font-family: sans-serif;
}

/*Add a card button*/
#new-item {
    position: relative;
    left: 40px;
    top: 20px;
    font-family: sans-serif;
    padding: 4px;
    width: 100px;
    background-color: #ffffff;
    border-radius: 4px;
}

/*User input*/
#task {
    position: relative;
    left: 25px;
    top: 20px;
    height: 20px;
    width: 200px;
    padding: 10px;
    border-radius: 4px;
    padding-left: 4px;
}

.column {
    background-color: #E3E3E3;
    min-height: 75px;
    width: 25%;
    display: inline-block;
    position: relative;
    margin: 5px;
    vertical-align: top;
    top: 75px;
    right: 287px; 
    border-radius: 5px;
}

.column h1 {
    font-size: 20px;
    padding-left: 10px;
    position: relative;
    color: #393939;
    margin: 5px;
}

li {
    list-style-type: none;
    margin-left: 8px;
    text-indent: 10px;
}

li.addedTask {
    border: 1px solid white;
    border-radius: 4px;
    padding: 15px;
    width: 83%;
    background-color: white;
    margin-bottom: 15px;
}

/*Cards in the todo list*/
#started {
    float: left;
    background-color: #E65C5C;
    border-radius: 6px;
    border: none;
}
<head>

    <title> HW03 Javascript and jQuery </title>
    <link rel="stylesheet" href="_css/style.css">
</head>
    <body>
        <header>
            <a href="https://trello.com"></a>
        </header>

        <section>
            <!-- create input tag for user input -->
            <input type="text" id="task">


            <!-- button takes input and adds a new element with content to 'list_do' -->
            <button id="new-item"> Add a card </button>


            <!-- ability to move items between list_todo and list_doing -->
            <div class="column" id="to-do">
                <h1> To Do </h1>
                <li id="todo-list"></li>
            </div>

            <div class="column" id="doing">
                <h1> Doing </h1>
                <li id="doing-list"></li>
            </div>

        </section>
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="_js/main.js"> </script>
    </body>

1 个答案:

答案 0 :(得分:0)

你可以这样做:

&#13;
&#13;
// Dynamically creates a new li element with 'started' button after user puts in text and clicks a button similar to 'submit'
$(document).ready(
  $("#new-item").on('click', function() {
    // once the document loads, create new item with this function
    var text = $('#task').val();
    if (text != '') {
      $('#todo-list').prepend("<li class='addedTask unmark'> <button id='started' value='on'>Started</button>" + text + '</li>');
    }

  })
);

// Clicking on the 'started' button will move the task from 'todo-list' to 'doing-list'
$("#todo-list").on('click', "button", function() {

  var completedItem = $(this).parent();
  change(completedItem, true);
  $('#doing-list').prepend($(completedItem));
});

// Clicking on the 'unmark' button will moe the task back from 'doing-list' to 'todo-list'
$("#doing-list").on('click', "button", function() {
    var completedItem = $(this).parent();
    change(completedItem, false);
    $('#todo-list').prepend($(completedItem));
});
function change(selector, isMarked) {
    if(isMarked) {
        $(selector).removeClass('unmark');
        $(selector).addClass('mark');
    } else {
        $(selector).removeClass('mark');
        $(selector).addClass('unmark');
    }
}
&#13;
header {
	background-color: #026aa7;
	height: 30px;
	text-align: center;
	padding: 5px 8px;
}

header a {
	height: 30px;
	width: 80px;
	text-align: center;
	background-image: url(https://a.trellocdn.com/dist/images/header-logo-2x.01ef898811a879595cea.png);
	background-repeat: no-repeat;
	text-align: center;
	display: inline-block;
	background-size: 80px 30px;
}

body {
	background-color: #0078c0;
	margin: 0px;
    font-family: sans-serif;
}

/*Add a card button*/
#new-item {
    position: relative;
    left: 40px;
    top: 20px;
    font-family: sans-serif;
    padding: 4px;
    width: 100px;
    background-color: #ffffff;
    border-radius: 4px;
}

/*User input*/
#task {
    position: relative;
    left: 25px;
    top: 20px;
    height: 20px;
    width: 200px;
    padding: 10px;
    border-radius: 4px;
    padding-left: 4px;
}

.column {
    background-color: #E3E3E3;
    min-height: 75px;
    width: 25%;
    display: inline-block;
    position: relative;
    margin: 5px;
    vertical-align: top;
    top: 75px;
    right: 287px; 
    border-radius: 5px;
}

.column h1 {
    font-size: 20px;
    padding-left: 10px;
    position: relative;
    color: #393939;
    margin: 5px;
}

li {
    list-style-type: none;
    margin-left: 8px;
    text-indent: 10px;
}

li.addedTask {
    border: 1px solid white;
    border-radius: 4px;
    padding: 15px;
    width: 83%;
    background-color: white;
    margin-bottom: 15px;
}

/*Cards in the todo list*/
#started {
  float: left;
  border-radius: 6px;
  border: none;
}
.mark button {
  background-color: green;
}
.unmark button {
  background-color: #E65C5C;
}
&#13;
<head>

    <title> HW03 Javascript and jQuery </title>
    <link rel="stylesheet" href="_css/style.css">
</head>
    <body>
        <header>
            <a href="https://trello.com"></a>
        </header>

        <section>
            <!-- create input tag for user input -->
            <input type="text" id="task">


            <!-- button takes input and adds a new element with content to 'list_do' -->
            <button id="new-item"> Add a card </button>


            <!-- ability to move items between list_todo and list_doing -->
            <div class="column" id="to-do">
                <h1> To Do </h1>
                <li id="todo-list"></li>
            </div>

            <div class="column" id="doing">
                <h1> Doing </h1>
                <li id="doing-list"></li>
            </div>

        </section>
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="_js/main.js"> </script>
    </body>
&#13;
&#13;
&#13;

相关问题