使用URL触发onClick事件

时间:2013-05-09 23:48:41

标签: javascript triggers onclick accordion

我希望能够导航到一个页面并在手风琴js结构中的三个内容容器之一中打开内容。

这是html:

<div id="navigation">
<div id="AccordionContainer" class="AccordionContainer">
            <div onclick="runAccordion(1); ContentHeight=1040;" id="reel">
                <div class="AccordionTitle" onselectstart="return false;">REEL</div>
            </div>
            <div id="Accordion1Content" class="AccordionContent">
                <div id="reelSpots">
                    content
                </div>
            </div>
            <div onclick="runAccordion(2); ContentHeight=1075;">
                <div class="AccordionTitle" onselectstart="return false;">ABOUT</div>
            </div>
            <div id="Accordion2Content" class="AccordionContent">
                content
            </div>
            <div onclick="runAccordion(3); ContentHeight=175;">
                <div class="AccordionTitle" onselectstart="return false;">CONTACT</div>
            </div>
            <div id="Accordion3Content" class="AccordionContent">
                content
            </div>
        </div>
    </div>

这是我的javascript:

// JavaScript Document

var ContentHeight = 200;
var TimeToSlide = 250.0;

var openAccordion = '';

function runAccordion(index)
{
  var nID = "Accordion" + index + "Content";
  if(openAccordion == nID)
    nID = '';

  setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'"
      + openAccordion + "','" + nID + "')", 33);

  openAccordion = nID;
}

function animate(lastTick, timeLeft, closingId, openingId)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;

  var opening = (openingId == '') ? null : document.getElementById(openingId);
  var closing = (closingId == '') ? null : document.getElementById(closingId);

  if(timeLeft <= elapsedTicks)
  {
    if(opening != null)
      opening.style.height = ContentHeight + 'px';

    if(closing != null)
    {
      closing.style.display = 'none';
      closing.style.height = '0px';
    }
    return;
  }

  timeLeft -= elapsedTicks;
  var newClosedHeight = Math.round((timeLeft/TimeToSlide) * ContentHeight);

  if(opening != null)
  {
    if(opening.style.display != 'block')
      opening.style.display = 'block';
    opening.style.height = (ContentHeight - newClosedHeight) + 'px';
  }

  if(closing != null)
    closing.style.height = newClosedHeight + 'px';

  setTimeout("animate(" + curTick + "," + timeLeft + ",'"
      + closingId + "','" + openingId + "')", 33);
}

我希望能够在URL字符串中添加#reel并能够导航到并打开Accordion1Content。

1 个答案:

答案 0 :(得分:1)

您可以使用JQuery的页面准备功能以及URL解析来触发点击事件: 假设您的网址如下:http://mysite.com/?page=reel

$(function() {
    var url = window.location.href;
    var page = url.match(/page=([^\?]+)/)[1];
    if (page=="reel") { runAccordion(1); }
});

您也可以为其他页面名称添加案例。