开发者

Is it possible to fire an onclick event in an url or query string?

I have a w开发者_运维百科eb page with a off-site links whose onclick events are handled by jQuery. The jQuery fetches the content of the page the link points to and inserts it into a div.

What I would like to do is be able to give somebody an url for this page that would cause the onclick event for one of the links to fire, thus loading the div with the remote page.

For example, say my page has the following markup:

<a class=link href=yahoo.com>yahoo</a>
<a class=link href=bekko.com>bekko</a>
<div id=div_where_remote_html_is_loaded></div>

...and jQuery code like:

$('a.link').click(function(){
  //code fetches remote html and adds to div
  }

Is it possible to specify an url to this page that would also cause the onclick event to fire for one of the links?


You could add an id to your links and use it as a hash on the url you want to give ..

then use javascript to target the link in your page..

<a class=link href=yahoo.com id="link1">yahoo</a>
<a class=link href=bekko.com id="link2">bekko</a>
<div id=div_where_remote_html_is_loaded></div>

and after you assign the click event with your code add

$('#' + location.hash ).click();

The link you would give now would be the normal url with a #link1 at the end for the first link, like http://somedomain.ext/yourpage.htm#link1 ..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜