使用Javascript在SharePoint中将文本转换为HTML [英] Text to HTML in SharePoint using Javascript

查看:111
本文介绍了使用Javascript在SharePoint中将文本转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SharePoint 2007中有一个摘要单行文本列,它是多行文本列的截断.通过复杂的过程到达那里,它将变成文本,然后需要将其转换回HTML,这样就不会显示诸如<div>的标记.如果多行列是富文本格式,则以下代码有效,但如果它是增强型富文本格式,则以下代码无效.有没有人方便的代码可以完成这项工作? (注意:我正在研究它,但到目前为止尚未真正完成任何javascript,因此进展缓慢).

I have a summary single-line text column in SharePoint 2007 that is a truncation of a multi-line text column. Going through the complicated process to get there, it turns into text which then needs to be converted back to HTML, so that the tags like <div> don't show. The following code works if the multi-line column is rich text, but not if it's enhanced rich text. Does anyone have the code handy to make this work? (Note: I am working on it but haven't really done any javascript up until now, so it's slow going).

<script type="text/javascript">
  var theTDs = document.getElementsByTagName("TD");
  var i=0;
  var TDContent = " ";
  while (i < theTDs.length)
  {
    try
    {
      TDContent = theTDs[i].innerText || theTDs[i].textContent;
      if (TDContent.indexOf("<div") == 0)
        {
          theTDs[i].innerHTML = TDContent;
        }
    }
  catch(err){}
  i=i+1;
  }
</script>

我现在得到的结果几乎看不到,因为使用增强型富文本格式,div标签的长度超过了我的45个字符的截断限制.

The result I'm getting now is nothing visible, because with enhanced rich text the div tag is longer than my 45 character truncation limit.

推荐答案

如何使用Christophe的

How about using Christophe's techniques to output HTML using a calculated column.

具体来说,他编写了JavaScript,可以将已编码的HTML(您现在拥有的HTML)转换为HTML.

Specifically he has written javascript that will turn the encoded HTML (which you've now got) into HTML.

在同一页面上的内容编辑器Web部件(CEWP)中添加以下内容.

Add the following into a Content Editor Web Part (CEWP) on the same page.

<script type="text/javascript">
/*
Text to HTML Lite - version 2.1.1
Questions and comments: Christophe@PathToSharePoint.com
*/

function TextToHTML(NodeSet, HTMLregexp) {
   var CellContent = "";
   var i=0;
   while (i < NodeSet.length)
   {
      try 
      {
         CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
         if (HTMLregexp.test(CellContent)) 
            { NodeSet[i].innerHTML = CellContent; }
      } 
      catch(err)
      {}

      i=i+1;
   }
}

// Calendar views
var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*");
TextToHTML(document.getElementsByTagName("a"),regexpA);

// List views
var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
TextToHTML(document.getElementsByTagName("TD"),regexpTD);

</script>

这篇关于使用Javascript在SharePoint中将文本转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆