简体   繁体   English

使用URL触发onClick事件

[英]Trigger onClick event with URL

I would like to be able to navigate to a page and open up content in one of three content containers in an accordion js structure. 我希望能够导航到页面并在可折叠js结构中打开三个内容容器之一中的内容。

Here is the html: 这是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>

And here is my javascript: 这是我的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);
}

I'd love to be able to put a #reel in the URL string and be able to navigate to, and open Accordion1Content. 我希望能够在URL字符串中放入#reel,并能够导航到并打开Accordion1Content。

You can use JQuery's pageready function, along with URL parsing to trigger a click event: Say your URL looks like: http://mysite.com/?page=reel 您可以使用JQuery的pageready函数以及URL解析来触发点击事件:说您的URL看起来像: http://mysite.com/?page=reel ://mysite.com/?page=reel

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

You could add cases for the other page names as well. 您也可以为其他页面名称添加大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM