如何突出显示点击链接的相应标题?

时间:2010-04-27 19:07:03

标签: javascript xml xslt

我正在编写一个XSL文件,其中包含一个带有链接列表的侧面导航菜单。当用户单击其中一个链接时,页面将跳转到该链接的相应信息表。如何才能使其在单击链接时突出​​显示该表的标题(而不是链接本身)?如果单击另一个链接,它也应该不突出显示。

以下是链接菜单:

<div onclick = "highlight(this);" onblur = "undoHighlight(this);">
<a href = "#{generate-id(.)}">
<xsl:value-of select = "."/> (<xsl:value-of select = "count(../n1:entry)"/>)
</a>
</div>

这是highlight / undoHighlight函数的javascript:

function highlight(link)
{
     undoHighlight(link)
     link.style.background = "red";
}
function undoHighlight(link)
{
     link.style.background = "white"; 
}

任何帮助将不胜感激。提前致谢!

编辑:这是表格的通用模板

<!-- Component/Section -->    
<xsl:template match="n1:component/n1:section">
    <xsl:apply-templates select="n1:title"/>
    <xsl:apply-templates select="n1:text"/>
    <xsl:apply-templates select="n1:component/n1:section"/>    
</xsl:template>

<!--   Title  -->
<xsl:template match="n1:title">
<div id = "dataTitleStyle">      
    <a name="{generate-id(.)}"><xsl:value-of select="."/></a>
</div>
</xsl:template>

<!--   Text   -->
<xsl:template match="n1:text">  
<xsl:apply-templates />
</xsl:template>

1 个答案:

答案 0 :(得分:0)

我使用jquery提供了一个示例输出页面,以突出显示菜单。

你必须在jquery中更改选择器,因为我现在没有页面中的所有周围元素。希望这有助于或至少为您提供灵感; - )

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
              // Add a click handler on in-page links 
              // You'd better provide another selector but I don't know the surrounding elements. I selected all a elements that contain a '#' inside a div.
              $('div a[href*=#]').click(function() {
                var theID = $(this).attr('href');
                var targetElementName = theID.substring(theID.indexOf('#') + 1)
                // "unhighlight" other elements
                // Set bg of all datatitlestyle elements to blue
                $('.dataTitleStyle > a[name]').parent().css('background','blue');
                // highlight the element
                // Set bg of clicked datatitlestyle element to red
                $('.dataTitleStyle > a[name=' + targetElementName + ']').parent().css('background','red');
              });
            });
        </script>
    </head>
    <body>
        <!-- The menu -->
        <div>
            <a href="#this_is_the_id">
                The output of xslt 1
            </a>
            <br />
            <a href="#this_is_the_second_id">
                The output of xslt 2
            </a>
        </div>
        <!-- Content -->
        <div class = "dataTitleStyle" style="background: blue;">      
            <a name="this_is_the_id">A title</a>
        </div>
        <div class = "dataTitleStyle" style="background: blue;">      
            <a name="this_is_the_second_id">A second title</a>
        </div>
    </body>
</html>