Automatically scroll to Element in HTML

2019-08-07 08:21发布

I have a XML containing loads of Test data. I use XSLT to present this data in HTML in a Browser. Each Test has a table containing the Teststeps. Every time a test is run the table updates the data in the table if passed or not. Then the browser reloads the page. I would like it to always jump to the table it is working on at the moment. How can I achieve this? My XSLT File:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
  <head>
  <script type="text/javascript">


    var testnum = 0;
     if(document.cookie.indexOf('testnum=') !== -1) {

     testnum = document.cookie.split('testnum=')[1].split(';')[0];

    document.cookie = 'testnum=' + testnum + 1 + ';expires=Sat, 14 May 2015 18:00:00      GMT";';
   } else {

      document.cookie = 'testnum=1;expires=Sat, 14 May 2015 18:00:00 GMT";';
   }

var w = $(window);
var row = $('#test').find('tr').eq( testnum );

if (row.length){
    w.scrollTop( row.offset().top - (w.height()/2) );
}

</script>

  <title>HMI Automotive Testing</title>
  <link rel="stylesheet" type="text/css" href="report.css" />

  </head>

  <body>

    <center>
    <h1>HMI Automotive Testing</h1>

      <br></br>
      <div style="height:650px;width:824px;border:1px solid;overflow:auto;">

            <table border="1" id ="test">
            <xsl:for-each select= "TestSuite/TestCase">

                        <tr>
                        <b><xsl:value-of select="@name"/></b>

                        </tr>

                        <xsl:for-each select="Verification|Command">
                                <tr>
                                <xsl:choose>
                                        <xsl:when test="contains(name() , 'Verification')">

                                        <td>Verification <xsl:value-of select="@type"/></td>
                                        <td><xsl:value-of select="@status"/></td>
                                </xsl:when>
                                <xsl:when  test="contains(name() , 'Command')">
                                        <td>Command <xsl:value-of select="@type"/></td>
                                        <td><xsl:value-of select="@status"/></td>
                                </xsl:when>
                                </xsl:choose>

                                </tr>


                        </xsl:for-each>

              </xsl:for-each>
              </table>  
            </div>
      </center>



  </body>
  </html>

</xsl:template>

</xsl:stylesheet> 

标签: java html xml xslt
2条回答
beautiful°
2楼-- · 2019-08-07 08:47

Use id attribute.

<table id="result1">
...
</table>

Then if you redirect page to your_page.html#result1 browser will scroll to this table.

查看更多
爷的心禁止访问
3楼-- · 2019-08-07 08:52

You need to introduce some state, so that the browser can know which test it's the currently on. You can do this in several different ways:

URL

If you can add a parameter to the URL each time a test is run, then you should do what @psur said.

Cookies

Use a Cookie to store the current test number.

While the XSLT is outputting the HTML page, have it output some js at the top too. This needs to check for the existence of a cookie, and increment it's value each time the page is loaded, then scroll to that position in the HTML. Something like this:

<script type="text/javascript">
//<![CDATA[

var testnum = 0;
if(document.cookie.indexOf('testnum=') !== -1) {
    // Cookie already exists, this isn't the first test, read the testnum
    testnum = document.cookie.split('testnum=')[1].split(';')[0];
    // write to next testnum back to the cookie for next time.
    document.cookie = 'testnum=' + testnum + 1 + ';expires=Sat, 14 May 2015 18:00:00 GMT";';
} else {
    // No cookie, must be first test
    document.cookie = 'testnum=1;expires=Sat, 14 May 2015 18:00:00 GMT";';
}
// scroll to the correct test table
location.hash = '#' + testnum;

//]]>
</script>

I've given the cookies a one year expiry because I don't know what you're doing about sessions, browser restarts, etc... By default cookies last for the session, so you might be able to get away with that.


In addition to one of those things, you also need to do what @psur said, i.e. add an id to each table so that the browser can scroll to it. I would suggest just using the number:

<table id="1">
...
</table>
查看更多
登录 后发表回答