簡體   English   中英

用jQuery剝離HTML標簽

[英]Stripping HTML tags with Jquery

示例: http//jsfiddle.net/axfsD/1/

我正在嘗試剝離頁面中的HTML標簽。 我在下面還粘貼了兩個示例。 通過使用replace命令,我不能剝離第一個按鈕的標簽,但是我不能剝離第二個按鈕的標簽。 可能是什么問題?

< script >
    $('button').replace(/<.*?>/g, "");
< /script>​

<button class="btn btn-info" rel="popover" data-content="hic   
aslinda sen de olabilir " data-original-title="Gemlik Bursa">
<small><span title="">mihan tosun</span></small></button>

<button class="btn btn-info" rel="popover" 
data-content="Benim olan benimdir Baskasinda gorduysen bil ki vazgecmisimdir   " 
data-original-title=""> <small><span title="">
Senin TAMER</span></small></button>​​​

為什么要這樣做? 也許$.text() [ 鏈接到doc ]可以滿足您的需求。

您的問題是因為replace()是本機JS函數,而不是jQuery函數。 因此,它不會在所有按鈕上重復。 這是另一種更健壯的方法。

$('button').each(function(){
    var buttonText = $(this).text();
    $(this).empty().text(buttonText);
});

不要將正則表達式與HTML一起使用。 認真地說, 就是不要

如果只需要文本,請使用text()方法。 據我了解,這就是您想要的:

$( 'button' ).each( function( index, value ) {
    $( this ).html = $( this ).text()
} )

我不會在按鈕內使用html標簽。 如果需要更改按鈕文本的外觀,可以使用css。 然后,您可以使用.html()jquery函數獲取按鈕的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM