jquery를 사용하여 부모의 기존 ul에 추가하기 위해 입력에서 텍스트를 가져 오려고하는데 이렇게 할 때 :
$("input[type = 'text']").keypress(function(event){
if(event.which === 13){
//grabbing new todo from input
var todoText = $(this).val();
$(this).val("");
//create a new li and add to ul
$(this).parent().append("<li><span><i class='fa fa-trash'></i></span> " + todoText + "</li>");
}
});
부모에게 직접 새로운 li을 추가합니다. 기존 ul에 추가하고 싶다는 언급을 잊었 기 때문에 변경해 보았습니다.
//create a new li and add to ul
$(this).parent("ul").append("<li><span><i class='fa fa-trash'></i></span> " + todoText + "</li>");
하지만 여전히 작동하지 않았고 이제는 아무데도 추가하지 않습니다. 구문이 잘못 되었기 때문일까요?
다음은 내 옥이 어떻게 구성되는지에 대한 예입니다.
.frame
.bit-2
h1
| text
i.fa.fa-plus
input(type='text', placeholder='text')
ul
li
span
i.fa.fa-trash
| text
li
span
i.fa.fa-trash
| text
무슨 일인지 아십니까?
아래와 같이 .next () 함수를 사용하세요.
$(document).ready(function(){
$("input[type = 'text']").keypress(function(event){
if(event.which === 13){
//grabbing new todo from input
var todoText = $(this).val();
$(this).val("");
//create a new li and add to ul
$(this).next("ul").append("<li><span><i class='fa fa-trash'></i></span> " + todoText + "</li>");
}
});
});
데모는 아래 스 니펫을 확인하십시오.
$(document).ready(function(){
$("input[type = 'text']").keypress(function(event){
if(event.which === 13){
//grabbing new todo from input
var todoText = $(this).val();
$(this).val("");
//create a new li and add to ul
$(this).next("ul").append("<li><span><i class='fa fa-trash'></i></span> " + todoText + "</li>");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text"/>
<ul><li>First</li></ul>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다