【问题标题】:Get second body text using javascript使用 javascript 获取第二个正文文本
【发布时间】:2017-09-28 06:00:39
【问题描述】:

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

我已经尝试如下 get body tag value from website Get all elements in the body tag using pure javascript

第二个body 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>

我也尝试使用如下类名,但显示 $(...).text 不是函数。

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

请提出您的建议.. 谢谢

【问题讨论】:

  • 你确定你已经加载了 jQuery 吗?
  • .text() 函数有效。我想知道的是body标签是否会被删除......

标签: javascript jquery html rich-text-editor wysihtml5


【解决方案1】:

尝试添加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>

【讨论】:

  • 哇它的工作..感谢您的快速回复..我已经尝试过您提到的 javascript document.getElementsByTagName("body")[0].textContent
猜你喜欢
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多