【问题标题】:How to convert the html object to string type?如何将 html 对象转换为字符串类型?
【发布时间】:2012-09-02 21:57:31
【问题描述】:

我使用 jQuery 方法来获取某种类型的 html 对象:

var content = $('#cke_ckeditor iframe').contents().find('.cke_show_borders').clone();

那我想转换成string类型:

console.log(content[0].toString());

但结果是:

[object HTMLBodyElement]

我怎样才能把它变成真正的字符串?

对了,可以把转换后的html字符串转成html对象吗?

【问题讨论】:

    标签: string html-parsing


    【解决方案1】:

    相信你想用Element.outerHTML:

    console.log(content.outerHTML)

    【讨论】:

    • 这向我证明它是有效的。 console.log(JSON.stringify(content[0].outerHTML))
    • 这行得通 - 我被困在尝试使用 .toString()JSON.stringify 没有运气
    • typeof content.outerHTML 仍然显示对象
    【解决方案2】:

    你可以用这个content[0].prop('outerHTML')

    它对我有用

    参考:How do you convert a jQuery object into a string?

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。

      var docString = "<html>"+content.documentElement.innerHTML+"</html>"

      【讨论】:

        【解决方案4】:

        您可以尝试以下方法:

        content.text();
        

        【讨论】:

          【解决方案5】:

          将jQuery对象转换为字符串的正确方法:

          var content = $('#cke_ckeditor').find('.cke_show_borders').eq(0).clone();

          console.log(content.get(0).outerHTML);

          【讨论】:

            【解决方案6】:

            'outerHTML' 为我工作。

            在 Google 跟踪代码管理器的“自定义 javasctipt”部分 {{Click Element}} 返回 [object HTMLElement]。

            function() {
               var elm = {{Click Element}};
               return elm.outerHTML; // working
            }
            

            结果 -

            <i _ngcontent-c1="" class="material-icons">edit</i>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-06-23
              • 1970-01-01
              • 2018-09-12
              • 2018-12-08
              • 1970-01-01
              • 2011-10-02
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多