I just can not figure a way to use location.href to act like you're clicking on a link. What I am trying to do is get the variables included, for example,
The regular link is like this and works fine:
<a href="images/someImage.jpg" title="My Image" rel="gb_page_center[249, 266]">Click Me</a>
I am trying to call a function using href (or some other method) to open the image with the other parameters just as if you were to click on the link.
I hope I am explaining this correctly.
Thanks for any help.
-UPDATE-
I finally found my solution here Displaying the Popup box generated by Greybox on page load(onLoad)
Thank you all for your suggestions.
回答:是的。
<a href="javascript:yourFunction(); return false;" title="My Image" rel="gb_page_center[249, 266]">Click Me</a>
Add an onclick event to your link. Using JQuery, you could ofcourse bind the click event to the links, and use .preventDefault() to block the anchor tag workin. Much cleaner code also.
$(document).ready(function(){
$('a').bind('click', function(e){
e.preventDefault();
//do whatever you wish to do
});
});
<a onclick="yourJSFunction();return false" href="images/someImage.jpg" title="My Image" rel="gb_page_center[249, 266]">Click Me</a>
You just need to add an onclick
attribute to your anchor tag.
Make sure to return false
or else the anchor tag will go to the URL like normal.
location.href
or window.location.href
refers to the browsers current address. If you change the value of location.href
the browser will use that value as it's address. So if you want to navigate your browser to a link programmatically, you just have to change its value.
If you just want a clickhandler for your link don't use onclick or href, that's just bad practice, use a sepperate script that does this unobtrusively like so:
var button = document.getElementById('button');
button.onclick = function(){
do_something();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.