我使用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