2016-09-09 2 views
1

목록에 항목을 추가하는 작은 앱이 있습니다. 항목 옆에 단추가 표시되며 해당 단추를 눌러서 (텍스트 장식 : 줄 넘김)을 추가 할 수있게하려고합니다. 나는 몇 가지 시도했지만 아무것도 작동하지 않는 것 (항목을 추가하는 자바 스크립트, 마지막 항목을 삭제하고 새로운 li 요소에 클래스를 추가하는 등 모든 것이 잘 작동한다. 문제는 JQuery 부분과 더 많은 주석이있다. 코드 자체에서).HTML 목록, 선택한 항목을 교차하는 JQuery 추가하기.

HTML

<html> 

<body> 

<h1> Shopping List </h1> 


<button id="add"> Add </button> 

<input type="text" id="input" placeholder="Enter Items"> </input> 

<button id="remove"> Remove Last </button> 


<ul id="list"> 

</ul> 

</body> 

</html> 

JS/JQ :

document.getElementById("add").addEventListener('click', function() { 


var check = document.createElement("button"); 
var input = document.getElementById("input").value; 
var newEl = document.createElement("li"); 
var newText = document.createTextNode(input); 
var buttonText = document.createTextNode("Check"); 

newEl.className = "liEl"; 
newEl.appendChild(newText); 
newEl.appendChild(check); 
check.setAttribute("class", "checked"); 
check.appendChild(buttonText); 


/* Problem starts here */ 

$("button.checked").on('click', function() { 

$('li.liEl').css('text-decoration: line-through'); 

/* It should get the button with the class "checked" and on click, make the li elements with class "liEl" to have that css... */ 

} 
); 


var position = document.getElementsByTagName("ul")[0]; 
position.appendChild(newEl); 
document.getElementById("input").value = ""; 



document.getElementById('input').onkeypress = function(e) { 
if (e.keyCode == 13) { 
document.getElementById('add').click(); /* adds an event listener to the submit text, keyCode 13 equals the enter key so when it's pressed it presses the add button. */ 
} 
} 
}); 


/* Delete last item function: */ 

    document.getElementById("remove").addEventListener('click', function() { 
    var els = document.getElementsByTagName("li"); 
    var removeEl = els[els.length - 1]; // <-- fetching last el, If els is an array, it has indices from 0 to els.length - 1. 0 is the first, els.length - 1 is the last index. 
    var containerEl = removeEl.parentNode; 
    containerEl.removeChild(removeEl); 
}); 
+0

귀하의 사용은() 아마 잘못된 것입니다. 여기에서 시작하십시오 : http://api.jquery.com/css/#css2 – Chiu

+0

이상하지만 이상하게도 작동합니다. 목록에 1 개 이상의 항목이 추가되었을 때만 가능합니다. 한 항목 만있는 경우 효과를 적용 할 수 없습니다). – Sergi

+0

@Sergi DOM에 새 요소가 추가되기 전에 클릭 기능이 추가 되었기 때문입니다. 따라서 다른 항목을 추가하면 기능이 적용됩니다. – Dfirmansyah

답변

1
  1. , 당신은 두 개의 PA를 제공해야 css 값을 설정하는 녀석 (이 참조 : css-property-name-value 참조).
  2. 선택기 구문 ($('li.liEl'))이 맞지 않으면 클릭 한 버튼이 아닌 <li> 요소를 모두 반환합니다. 사용할 수 있습니다 : $(this).parent().css('text-decoration', 'line-through');.
  3. 코드에 버그가 있습니다. 마지막으로 추가 한 버튼은 기능을 트리거하지 않습니다. 클릭 기능이 보다 앞에 추가되어 새 요소가 DOM에 추가 되었기 때문입니다. 그리고 그것은 귀하의 클릭 기능이 이전에 추가 된 버튼에 대해 여러 번 트리거되도록합니다.

다음은 고정 코드 스 니펫입니다. 이미 jQuery를 사용하고 있기 때문에, 네이티브 자바 스크립트 네이티브 요소 쿼리 및 이벤트 처리기 whith jquery 구문을 변경합니다.

$(function() { 
 
\t $("#add").click(function(evt) { 
 
\t \t var input = $('#input').val(); 
 
\t \t var check = $('<button class="checked">Check</button>'); 
 
\t \t var newEl = $('<li class="liEl"></li>'); 
 
\t \t newEl.append(input); 
 
\t \t newEl.append(check); 
 
\t \t 
 
\t \t $(check).click(function(evt) { 
 
\t \t \t $(this).parent().css('text-decoration', 'line-through'); 
 
\t \t }); 
 
\t \t 
 
\t \t $('#list').append(newEl); 
 
\t \t $('#input').val(''); 
 
\t }); 
 
\t 
 
\t $('#remove').click(function(evt) { 
 
\t \t var lastEl = $('li.liEl').last(); 
 
\t \t lastEl.remove(); 
 
\t }); 
 
\t 
 
\t $('#input').keypress(function(evt) { 
 
\t \t if (evt.keyCode === 13) { 
 
\t \t \t $("#add").click(); 
 
\t \t } 
 
\t }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 

 
<body> 
 

 
<h1> Shopping List </h1> 
 

 
<button id="add"> Add </button> 
 
<input type="text" id="input" placeholder="Enter Items" /> 
 
<button id="remove"> Remove Last </button> 
 

 
<ul id="list"></ul> 
 

 
</body>
.CSS의

+0

완벽하게 작동합니다. 이후 개별적으로 항목을 교차하는 해결책을 찾고 싶었지만 코드를 연구 할 것입니다. 감사합니다. – Sergi

2

사용 스타일 $('li.liEl').css('text-decoration','line-through'); 같은 당신의 jQuery를 css 기능이 잘못

관련 문제