代码之家  ›  专栏  ›  技术社区  ›  Arun D

使用javascript获取第二个正文文本

  •  0
  • Arun D  · 技术社区  · 9 年前

    基本上HTML元素包含一个body标记。在这个body标签中,我们添加了所需的html内容。在我的例子中,HTML文件有两个主体内容,一个用于整个HTML内容,另一个用于富文本编辑器。我需要使用第二个正文获取富文本编辑器文本。

    我已经试过了 get body tag value from website Get all elements in the body tag using pure javascript

    第二个正文html内容如下,

    <body marginwidth="0" marginheight="0" spellcheck="true" class="wysihtml5-editor" style="font-variant-ligatures: normal; font-variant-caps: normal; background-color: rgb(255, 255, 255); color: rgb(95, 100, 104); cursor: auto; font-family: &quot;Open Sans&quot;, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; line-height: 18px; letter-spacing: normal; text-align: start; text-decoration: none solid rgb(95, 100, 104); text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px;" contenteditable="true">testing</body>
    

    我还尝试使用类名,如下所示,但显示了$(…)。文本不是函数。

    $(".wysihtml5-editor").text()
    

    谢谢

    1 回复  |  直到 9 年前
        1
  •  1
  •   adiga    9 年前

    尝试添加jquery库或使用javascript

    //by javascript
    var text = document.getElementsByTagName("body")[0].textContent
    alert(text);
    
    //by jquery
    $(document).ready(function(){
        var text = $('body').text();
        alert(text);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <body marginwidth="0" marginheight="0" spellcheck="true" class="wysihtml5-editor" style="font-variant-ligatures: normal; font-variant-caps: normal; background-color: rgb(255, 255, 255); color: rgb(95, 100, 104); cursor: auto; font-family: &quot;Open Sans&quot;, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; line-height: 18px; letter-spacing: normal; text-align: start; text-decoration: none solid rgb(95, 100, 104); text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px;" contenteditable="true">testing</body>