天天看點

jq如何删除html标簽,javascript – 從一個字元串中删除HTML标簽,使用jQuery

最簡單的形式(如果我正确了解):

var s = "

Hello World!

By Mars

";

var o = $(s);

var text = o.text();

或者您可以使用具有搜尋上下文的條件選擇器:

// load string as object,wrapped in an outer container to use for search context

var o = $("

Hello World!

By Mars

");

// sets the context to only look within o; otherwise,this will return all P tags

var tags = $("P",o);

tags.each(function(){

var tag = $(this); // get a jQuery object for the tag

// do something with the contents of the tag

});

如果您正在解析大量的HTML(例如,解釋螢幕截圖的結果),請使用伺服器端HTML解析庫,而不是jQuery(這裡有關于HTML解析的文章).

@[email protected]

@[email protected]

總結

如果覺得程式設計之家網站内容還不錯,歡迎将程式設計之家網站推薦給程式員好友。

本圖文内容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。