JavaScript 有类似 simple_html_dom.php 的东西吗?

发布于 2024-12-10 01:37:25 字数 505 浏览 0 评论 0原文

我正在使用 Appcelerator Titanium 构建 iPhone 应用程序,并且需要将 HTML 字符串(其中可能包含无效的 HTML,如缺少标签,这不是我的错)转换为 DOM 对象。在此 DOM 元素中,我需要找到特定的

,并将此
的内容放入 WebView 中。

不管怎样,我正在寻找类似于 Simple HTML DOM 脚本的东西,该脚本用于 PHP,通过 DOM 进行搜索对于这个

。我希望脚本扫描(无效)HTML 字符串,并获取
的innerHTML。

我怎样才能最好地在 JavaScript 中做到这一点?

I'm building an iPhone app using Appcelerator Titanium, and I need to convert a HTML string (which may contain invalid HTML like missing tags, not my fault) to a DOM object. In this DOM element I need to find a specific <div>, and put the contents of this <div> in a WebView.

Anyway, I am looking for something like the Simple HTML DOM script, which is for PHP, to search through the DOM for this <div>. I'd like the script to scan the (invalid) HTML string, and get the innerHTML of the <div>.

How can I best do this in JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-12-17 01:37:25

下面的代码是基本的字符串到 DOM 转换器。请参阅链接的答案以获取详细说明。

function string2dom(html, callback){    
    /* Create an IFrame */
    var iframe = document.createElement("iframe");
    iframe.style.display = "none";
    document.body.appendChild(iframe);

    var doc = iframe.contentDocument || iframe.contentWindow.document;
    doc.open();
    doc.write(html);
    doc.close();

    function destroy(){
        iframe.parentNode.removeChild(iframe);
    }
    if(callback) callback(doc, destroy);
    else return {"doc": doc, "destroy": destroy};
}

此代码片段不会净化 HTML。将执行脚本,将加载外部源。代码的解释和 HTML 清理程序可以在 这个答案

用法(示例),小提琴:http://jsfiddle.net/JFSKe/

string2dom("<html><head><title>Test</title></head></html>", function(doc, destroy){
    alert(doc.title); /* Alert: "Test" */
    destroy();
});

var test = string2dom("<div id='secret'></div>");
alert(test.doc.getElementById("secret").tagName); /* Alert: "DIV" */
test.destroy();

The code below is the basic string-to-DOM convertor. See the linked answer for a detailed explanation.

function string2dom(html, callback){    
    /* Create an IFrame */
    var iframe = document.createElement("iframe");
    iframe.style.display = "none";
    document.body.appendChild(iframe);

    var doc = iframe.contentDocument || iframe.contentWindow.document;
    doc.open();
    doc.write(html);
    doc.close();

    function destroy(){
        iframe.parentNode.removeChild(iframe);
    }
    if(callback) callback(doc, destroy);
    else return {"doc": doc, "destroy": destroy};
}

This code snippet does not sanitise the HTML. Scripts will be executed, external sources will be loaded. The explanation of the code, and HTML sanitiser can be found at this answer

Usage (examples), Fiddle: http://jsfiddle.net/JFSKe/:

string2dom("<html><head><title>Test</title></head></html>", function(doc, destroy){
    alert(doc.title); /* Alert: "Test" */
    destroy();
});

var test = string2dom("<div id='secret'></div>");
alert(test.doc.getElementById("secret").tagName); /* Alert: "DIV" */
test.destroy();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文