2013-07-08 1 views
-2

이 HTML에는 백엔드 액세스 권한이 없으므로 JavaScript 만 사용할 수 있습니다.jQuery 전후에 스팬 추가 상위 집합 안에 숫자 집합

.before() 및 .after() 함수를 사용해 보았지만 첫 번째 전화 번호 앞과 마지막 번호 뒤에 만 가져올 수있었습니다.

다음은 그 모습입니다.

<div id="phonenumber">888-888-888 888-888-8888</div> 

이것은 내가 달성하고자하는 것입니다.

<div id="phonenumber"> 
<span>888-888-888</span> 
<span>888-888-8888</span> 
</div> 

답변

3

이렇게하면?

Demo

$('#phonenumber').html(function(_, html){ //Use html callback function 
    return $.map(html.split(/\s+/), function(val){ // split the current text based on spaces and use $.map to get the html span. 
     return $('<span/>',{text:val}); //construct the span. 
    }); 
}); 

는 출력 :

<div id="phonenumber"> 
     <span>888-888-888</span> 
     <span>888-888-8888</span> 
</div> 
+0

그것은 여전히 ​​첫 번째 전화 번호 앞에 마지막 한 후 범위를 추가합니다. – Chozen

+0

Nopes는 데모에서보기 소스를 봅니다. – PSL

+0

흠, 웬일인지 그것은 공간을 읽지 않고있다. – Chozen