目前我有一堆 HTML 存储在一个变量中,我将其输出到看起来有点像这样的页面:
<p class="firstpara">some stuff</p>
<p class="subhead">a heading</p>
<p class="subsubhead">a subheading</p>
<p>wording and such</p>
<p class="subsubhead">another subheading</p>
你明白了。
输出如下:
<cfoutput>
#request.oEntry.getHTMLStuff()#
</cfoutput>
无论如何,我需要找到“subsubhead”的所有类,并添加 id="x",其中 x 对应于该子标题的数据库 ID。
我认为这最好在 Coldfusion 中完成,因为数据库 ID 已经保存在 cfquery 中,并且我无法混合和匹配 jQuery 和 ColdFusion。
目前不确定执行此操作的最佳方法。
由于您喜欢 jQuery 但需要在 CF 中执行此操作,我建议使用 HTML 解析器执行此操作JSOUP http://jsoup.org。 JSOUP 的语法与 jQuery 非常相似,但使用 java(以及 CF)在服务器端运行。下载 jar 并将其添加到 CF 类路径后,您可以像这样使用它:
<cfset jsoup = CreateObject("java", "org.jsoup.Jsoup")>
<cfsavecontent variable="html">
<p class="firstpara">some stuff</p>
<p class="subhead">a heading</p>
<p class="subsubhead">a subheading</p>
<p>wording and such</p>
<p class="subsubhead">another subheading</p>
</cfsavecontent>
<cfset htmlObj = jsoup.parse(html)>
<cfloop array="#htmlObj.select('.subsubhead')#" index="element">
<cfif Find("a subheading", element.ownText())>
<cfset element.attr("id", 1)>
</cfif>
<cfif Find("another subheading", element.ownText())>
<cfset element.attr("id", 2)>
</cfif>
</cfloop>
<cfoutput>
<pre>
#HTMLEditFormat(htmlObj.body().html())#
</pre>
</cfoutput>
这输出:
<p class="firstpara">some stuff</p>
<p class="subhead">a heading</p>
<p class="subsubhead" id="1">a subheading</p>
<p>wording and such</p>
<p class="subsubhead" id="2">another subheading</p>
我将 id 值(通过检查 p 标签内容)分配给 subsubhead 的特定实例的方式只是一种选择;您还可以根据每个元素的顺序(数组中元素的顺序和查询中 id 的顺序)将元素与数据库查询进行匹配。由你决定。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)