我使用jQuery(和JS当然)编写的简单待办事项列表。 我已经创建了静态待办事项列表,只有通过编辑代码才能添加新项目。从逻辑上讲,我现在要创建一个动态列表。jQuery待办事项列表
我已经尝试了一些方法,如.load()代码从外部文件创建.after(),但这一切都出错了。
你会建议我做什么?
你会发现所有的源代码在strasbourgmeetings.org/ccc
我会很感激,如果你能帮助我解决这个问题。
Gloserio,是的,添加项目现在不工作,因为我所描述的问题的我。
ncubica,问题是,眼下我不能添加新项目到我的清单(仅BU编辑代码)。动态意味着可以添加/删除项目。要做到这一点,我尝试使用.after()
方法,里面的函数会复制<li id="item1">List item here</li><li id="buttons1">Buttons here</li>
(粗略地说),但它会将所有列表项放在上面,将所有按钮放在最下面。
这是JS代码的一部分:
<script>
// Waiting for the document to load
$(document).ready(function() {
// Wanted to create a dynamic item list, that's why I used this variable. I thought it would
// generate unique IDs (there was 'id++' somewhere in the function I already deleted).
var id = 1;
// This is going to be used as an action for 'Add Item' button. (It is not a button, actually, it is just <span> with cursor: pointer. Using <a> causes page reload);
$('.add').click(function() {
$('#item'+id).after(function(i) { // '#item'+id should be something like this in the code: #item2, #item3, etc.
})
})
// 'Done' button
$('#done1').click(function() {
console.log('# "Done" button pressed');
$('#list1').css('background-color','#89f49a').css('border','1px solid #16bf31').css('text-decoration','line-through').css('color','#817f7f').css('font-weight','normal');
console.log('# Item doned successfully');
});
// 'Undone' button (is gonna be renamed to 'Reset');
$('#undone1').click(function() {
console.log('# "Undone" button pressed');
$('#list1').css('background-color','').css('border','').css('text-decoration','').css('color','').css('font-weight','normal');
});
// 'Working' button
$('#working1').click(function() {
$('#list1').css('background-color','#edc951').css('border','1px solid #dda119').css('font-weight','bold').css('color','#000').css('text-decoration','none');
});
// 'Cancel' button
$('#cancel1').click(function() {
$('#list1').css('background-color','#ff8c8c').css('border','1px solid #ea2c2c').css('font-weight','normal').css('text-decoration','line-through').css('color','#f00');
});
// 'Delete' button
$('#del1').click(function() {
$('div#dlist1').remove();
$('li#action1').remove();
});
});
</script>
和HTML的一部分:
<div class="list">
<ul id="sortable">
<div class="list-item" id="item1"><div class="spacer1" id="spacer1"></div>
<div class="l-element" id="dlist1"><li id="list1" class="ui-widget ui-state-default">Create add feature</div>
<li id="action1" class="action"><input type="button" value="Done" class="done" id="done1"><input type="button" value="Undone" class="undone" id="undone1"><input type="button" value="Working" class="working" id="working1"><input type="button" value="Cancel" class="cancel" id="cancel1"><span id="del1" class="delete">Delete</span></li>
<div class="spacer"></div></div>
</ul>
<div>
正如你所看到的,只有1我写的列表项。 ID是静态的,目前无法更改。我只需要改变ID(好吧,它会是var id = 1; id ++)并添加部分代码(在<div class="list-item"
之内)
点击'加item',它什么都不做,是正常的吗?几个问题:你是什么意思的动态?你目前的问题在哪里? – Akheloes
对不起,我不明白你的问题是什么..请显示一些代码,我们可以帮助你 – ncubica