【发布时间】:2011-04-17 20:43:24
【问题描述】:
我有一个静态 HTML 页面,我在其中使用 span 标签和 javascript 来突出显示选定的文本部分。在一个单独的页面上,我想链接回这个 HTML 页面并指定突出显示处于活动状态。 请参阅下面提供的代码。
问题是必需的 style="background: transparent" 标签。它必须在那里,以便突出显示仅在单击时才处于活动状态,但这也意味着当我尝试如下指定链接到此页面时,突出显示不活动。
任何帮助将不胜感激。谢谢。
单击此链接会突出显示文档中指定的文本部分。
<span title="Warscape"><a title="Warscape" onclick="highlightSpan(this.getAttribute('title'))" href="">Warscape</a></span>
这是点击时突出显示的文本。
<span title="Warscape" class="highlight" style="background: transparent">During this month while we have been building Fort No 1 Spring field Missouri, quite a No of Regiments have arrived from the north & now the Army of the Frontier [is?] formed</span>
链接到带有突出显示的页面的代码。
<a href="j_62sept.html?highlight=Warscape">
CSS 重新。突出显示
.highlight {background:#bcdbda;}
Javascript 重新。突出显示
function highlightSpan(spanTitle)
{
var spans = document.getElementsByTagName("span");
// take away color
for (var i = 0; i < spans.length; ++i)
{
spans[i].style.backgroundColor = "transparent";
}
// add color
for (var i = 0; i < spans.length; ++i)
{
if (spans[i].getAttribute('title') == spanTitle)
{
spans[i].style.backgroundColor = "#bcdbda";
}
}
}
【问题讨论】:
标签: javascript css transparency highlighting html