Javascript获取HTML块中的最后一段文本

时间:2016-02-01 23:19:39

标签: javascript html sharepoint

这很简单,我确定但是我遇到了一些问题。我尝试使用Sharepoint 2013客户端渲染(CSR)来格式化包含长文本块的sharepoint字段,以仅显示最后一个段落块(想法是具有"最后更新状态&#34 ;对于报告最后一行条目的任务。)

所以,以这个html块为例:

<html>
  <head>
    <meta name="generator"
    content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" />
    <title></title>
  </head>
  <body>
    <div class="ExternalClass473CC6A801594594826FDFA0BFAD6B1E">
      <p>​</p>
      <p style="margin:0in;font-family:calibri;font-size:14pt;color:#0070c0;">
        <span style="font-weight:bold;text-decoration:underline;">Current Status</span>
        <span style="font-weight:bold;">: Pending</span>
      </p>
      <p style="margin:0in;font-family:calibri;font-size:12pt;">Some update</p>
      <p style="margin:0in;font-family:calibri;font-size:12pt;">Some Update 2</p>
      <p style="margin:0in;font-family:calibri;font-size:12pt;">THIS IS THE TEXT I WANT TO GET
      <br /></p>
      <p>
        <br />
      </p>
    </div>
  </body>
</html>

如何使用正则表达式或javascript html解析器设置变量以包含&#34;这是我想要的文本&#34;?

提前致谢! :)

6 个答案:

答案 0 :(得分:2)

通过“最后一个段落”你的意思是最后一个“p”元素?如果是这样,为什么不:

<script type="text/javascript">
  window.onload = function() {
    target = document.getElementsByTagName("p")[-1];
    content = target.innerText || target.textContent;
  };
</script>

在jQuery中:

<script type="text/javascript">
  jQuery(document).ready(function() {
    target = $("p").last();
    content = target.html();
  });
</script>

答案 1 :(得分:1)

您可以完全放弃使用JS并使用CSS执行此操作。将其粘贴到<head>

<style type="text/css">
p {
  display: none; /* This hides all p tags */
}
p:last-child {
  display: inline; /* This will show the last p tag */
}
</style>

答案 2 :(得分:1)

这里有一些时髦的正则表达式魔法

document
// Get the parent node
.querySelector('.ExternalClass473CC6A801594594826FDFA0BFAD6B1E') 
// Get the contents as text
.textContent
// Look for any character, then match all the way until there's a linebreak or its the end of the string. 
.match(/\w.+(\n|$)/g)
// Get the last matching item
.slice(-1)

答案 3 :(得分:0)

if (request.getSpeedInfo() == null
||
request.getSpeedInfo().getBandwidth() == null
||
request.getSpeedInfo().getBandwidth().isEmpty())
{
    // request is invalid
}
else
    // request is valid ...

答案 4 :(得分:0)

如果您想要为变量分配的<p>标记具有类似ID的内容,那么它可以很方便。

如果您这样做,您只需执行:var text = document.querySelector("#id").value

或者,如果这不是一个选项,您必须自己在<p>标签中查找。

类似于:var pTags = document.getElementByTagName("p"); var theTextIwant = ""; for(var i = 0; i < Ptags.length; i++){if(pTags[i] == "text i want; theTextIwant = pTags[i]; )}

答案 5 :(得分:0)

好的,所以我在最后一天花了这个,但看起来最简单,最安全的事情就是:

var bodyElements = doc.querySelectorAll("p");

//Find the last non-empty element
//TODO: Go back and try another element if all elements of the specified type are empty
var i = bodyElements.length;
var newBodyValue = "";
while (!(/\S/).test(newBodyValue) || i < 0) {
    i--;
    try {
        newBodyValue = bodyElements[i].innerText;
        if (!newBodyValue) {continue;}
    } catch (err) {
        continue;
    }
}

最后一个子选择器没有工作,因为我需要最后一个非空元素 period ,而不仅仅是每个层次结构的最后一个。我后来才知道我可以很容易地使用JQuery但是我试图保持它纯粹以保持Sharepoint的开销最小化。

我不得不添加一些额外的东西,但这里是最终的CSR JSLink结果。它需要一个标准的Sharepoint任务列表并修改description字段,以便它只显示输入的最后一行。这对于我希望信息尽可能紧凑的特定摘要视图很有用。

https://gist.github.com/JustinGrote/4fd3b5e7bf0420c283a9

感谢大家的建议。

我知道我可能并没有遵循很多方面的最佳做法,但我总共使用Javascript大约10个小时,所以起诉我:)