2014-07-17 4 views
3

스팬 외부의 텍스트를 가져 오려고하지만 스팬 내에 텍스트 만 가져올 수 있습니다.스팬 외부의 텍스트를 가져 오는 방법

<span class="text"> some text </span> text I want 

은 어떻게 당신은 부모에 그를 사용할 필요가 내가 jQuery를 사용하고 있지만 그것은 단지 "텍스트"

a = $('.text').contents().filter(function() { 
     return this.nodeType == 3; 
    }).text(); 
+0

'span'은 * 주변에 무엇입니까? –

+0

실제로 데이터를 선택하려는 위치에서 HTML 섹션을 제공하십시오. –

답변

1

를 얻을 수

"내가 원하는 텍스트"텍스트를 얻을 수 있습니다 스팬 http://jsfiddle.net/euvKK/

$(document).ready(function() { 
    var a = $(".parent").contents().filter(function() { 
     return this.nodeType == 3; 
    }).text(); 
    alert(a); 
}); 
2

생을 시도 ...

<div id="foo"><span>hello</span>i want</div> 

$(document).ready(function(){ 
    $value=$("#foo") 
    .clone() //clone the element 
    .children() //select all the children 
    .remove() //remove all the children 
    .end() //again go back to selected element 
    .text(); 
    alert($value); 
}); 

JS 바이올린 http://jsfiddle.net/qrASy/

0

이 당신 요소에 있지 모든 텍스트를 줄 것이다 ... 감사합니다. 즉 당신이 찾고 있던 무슨 있다면

http://jsfiddle.net/4QZGz/1/

HTML

<div> 
    <span class="text"> some text </span> text I want 
    <br/> 
    <div id="here"></div> 
</div> 

JS

a = $('.text'); 


$('#here').html(textAfter(a)); 

function textAfter(obj){ 
    var p=$(obj).parent(); 

// alert(/<\/\w+>(\s*[\w+\s+]+)<\w+>/.exec(p.html())); 
    return p.html().match(/<\/\w+>(\s*[\w+\s+]+)<\w+>/)[1]; 

} 
0
$('.parentClass:not(span)').method 

이, 스팬을 제외하고 모든게을 얻을 것이다.

관련 문제