click link with javascript
I have a link on my page that id like to click with the below 开发者_StackOverflow社区javascript function, but it isnt working. What I am really trying to do is use php to echo out a preclicked link. I think I have used the right function $(selector).click(), but I dont know where to put the link. I dont want to echo out the link, just the alert message. The link is actually a thickbox alert message, that is only able to be clicked on. I was hoping that i could use the .click() to activate it via php. thanks
<?php
echo "
<script type='text/javascript'>$('#link').click();</script>
<a href='wronginput.php?height=40&width=80' id='link' class='thickbox'>Link text</a>";
?>
At the time you output the javascript snippet, the link has not yet been parsed into the DOM, so $('#link')
returns a null object. Either wrap it in a .ready()
call, or place the javascript AFTER the link in your output.
<script>$(document).ready( function() { $('#link').click(); });</script>
<a href=...>
or
<a href=...>
<script...>
as Marc B said
<?php
echo "<a href='wronginput.php?height=40&width=80' id='link' class='thickbox'>Link text</a>
<script type='text/javascript'>$('#link').click();</script>
";
?>
or
<?php
echo "<script>$(document).ready( function() { $('#link').click(); });</script>
<a href='wronginput.php?height=40&width=80' id='link' class='thickbox'>Link text</a>
";
?>
精彩评论