2013-06-07 3 views
0

감안할 때 htmls : 이제JQuery와에 현재 선택한 요소의 outerHTML에 활용하는 방법을

<div id="t"> 
    <a class="xx">xx</a> 
    <a class="yy">yy</a> 
    <a class="zz">zz</a> 
</div> 

어떻게 그 클래스 xx 또는 zz있는 링크를 얻으려면, 그게 내가 얻고 싶은 말의에 대해 이 :

나는이 시도
<a class="xx">xx</a> 
    <a class="zz">zz</a> 

:

$("#t a").each(function(){ 
    if($(this).is(".xx,.yy"){ 
    //here how to get the out the HTML of the current a element? 
    } 
}); 

어떤 대안을?

두 가지 대답이 있지만 나는 그들이 오해하고있는 것 같습니다.

$("#t a").each(function(){ 
    if($(this).is(".xx,.yy"){ 
    //here I want to get the full HTML of the `a` element 
    // That's to say, I want to get `<a class="xx">xx</a>` 

    // I can not use $(this).html() or $(this).text() which will only return the `xx`. 
    } 
}); 
+0

내 업데이트 된 답변 확인 –

+0

http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html – rahularyansharma

답변

0
$("#t a.xx,#t a.yy").each(function(){ 
    $('<div>').append($(this).clone()).html(); 
}); 
1

다른 대답이 올바르지 않습니다. 여기 정확히 어떻게 얻을 수 있을까요? 앵커 HTML 얻기 위해 jQuery를 사용하여

$('a.xx,a.yy').each(function(index, currentLink){ 
    var z = currentLink.outerHTML; //z will be < 
}); 

또 다른 해결책을, 다음은

$('a.xx,a.yy').each(function(index, currentLink){ 
    alert($(currentLink.outerHTML).wrap('<p/>').parent().html()); 
}); 

http://jsfiddle.net/wabjw/

+0

'outerHtml'이 교차 브라우저로 작동합니까? – hguser

+0

100 % 확신 할 수는 없지만 jQuery를 이미 사용하고있는 것처럼 보이기 때문에 jQuery 솔루션을 포함하도록 답변을 업데이트했습니다. –

+0

firefox 3.x에서 작동하지 않습니다. – hguser

0

당신은

$("#t").find('.xx,.yy') 
1
var html = ''; 
$('#t a.xx, #t a.zz').each(function(){ 
    html += $(this).wrap('<p/>').parent().html(); 
    $(this).unwrap(); 
}); 

) (.find 시도 할 수 jsfiddle입니다 JS 피들 : http://jsfiddle.net/rwGsR/9/

0

이 정보가 맞습니까?

JS : 그것은 되돌아

$("#t a").each(function(){ 
    if($(this).is(".xx,.yy")){ 
    console.log(this.outerHTML); 
    } 
}); 

: 콘솔 "<a class=\"xx\">xx</a>" & "<a class=\"yy\">yy</a>".

관련 문제