2014-07-24 38 views
2

表明,它獲得元素相同的內容我試圖讓內部元素的內容,我想這是一樣的,當在瀏覽器中顯示如何當瀏覽器

<pre class="yyy">Hello this is <span style="display: none;"> test </span> text </pre> 

// how to alert (get) content like when it shown on browser: Hello this is text 
alert($(".yyy").html()); 

我想要提醒的結果是:Hello this is text與瀏覽器上顯示的一樣。那可能嗎?如何做到這一點,謝謝。

這裏是我的code

回答

2

我覺得this是你在找什麼。

jQuery.fn.visibleText = function() { 
    return $.map(this.contents(), function(el) { 
    if (el.nodeType === 3) { 
     return $(el).text(); 
    } 
    if ($(el).is(':visible')) { 
     return $(el).visibleText(); 
    } 
    }).join(''); 
}; 

alert($(".yyy").visibleText()); 

Here是小提琴。

4

我這樣做:

alert($(".yyy").clone().find(':not(:visible)').remove().end().text()); 

Demonstration

+0

這在這個例子中有效,但在我的實際應用程序中效果不好。 – DeLe