2017-10-20 3 views
1
<div class="ins"> test test test <div class="temp">123</div></div> 

스크립트는 단지 div을 숨기 클래스 "임시"대신 제거어떻게 JQuery와

$('.ins').text(function(){ 
return $(this).text().slice(0,5);}); 

답변

1

이 코드 조각을 시도하십시오 임시 클래스에서 텍스트를 제거하고 빈 사업부를두고

$(function(){ 
    $(".ins .temp").html(""); 
}) 

를 사용을 유지할 수 @ 무효의 코드 변경 이 코드는 주어진 DOM 구조에서만 작동합니다.

$('.ins').html(function(){ 
    return $(this).text().slice(0,5) + '<div class="temp">'+ $(this).find('.temp').text() +'</div>'; 
}); 
+0

감사합니다. – Sentimo

1

와 사업부를 삭제 DIV 요소를 제거하지 않고 텍스트를 자릅니다.

+0

"temp"클래스가있는 div가 표시되고 "test"텍스트 만 잘 렸습니다. – Sentimo

1

당신은 쉽게 간단하게 span 사업부 내부에이 작업을 수행 할 수 있습니다. 여기에서 div의 내용은 변수에 집어 넣었습니다.

var res = $('.ins .temp').html(); 
 
$('.ins .temp').html(""); 
 
$('#t').html(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="ins"> test test test <div class="temp">123</div></div> 
 
<h2 id="t"></h2>

1

$('.ins > span').text($('.ins > span').text().slice(0,5))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="ins"><span> test test test </span><div class="temp">123</div></div>
.temp DIV 내부의 값을 취득하고 동일한 div 요소에서 콘텐츠를 삭제 :

$(function(){ 
    $(".ins .temp").hide(); 
})