最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using an <a> onclick to open an ajax content into a fancybox - Stack Overflow

programmeradmin0浏览0评论

I'd like to do something like this:

<a href="javascript:void(0);" id="myLink" onclick="$(this).fancybox({href : 'myPage.php'});">Open ajax content</a>

Note that I MUST NOT use a script like this:

<script type="text/javascript">
   jQuery(document).ready(function(){
      $("#myLink").fancybox({href : 'myPage.php'});
   });
</script>

I want to trigger a fancybox from an <a> onclick element. But into this onclick function, it MUST have the 'href' from fancybox, exactly as I wrote above. But as I wrote, it doesn't work. Please help :)

I'd like to do something like this:

<a href="javascript:void(0);" id="myLink" onclick="$(this).fancybox({href : 'myPage.php'});">Open ajax content</a>

Note that I MUST NOT use a script like this:

<script type="text/javascript">
   jQuery(document).ready(function(){
      $("#myLink").fancybox({href : 'myPage.php'});
   });
</script>

I want to trigger a fancybox from an <a> onclick element. But into this onclick function, it MUST have the 'href' from fancybox, exactly as I wrote above. But as I wrote, it doesn't work. Please help :)

Share Improve this question edited Aug 4, 2012 at 6:36 Felipe Sabino 18.2k6 gold badges79 silver badges112 bronze badges asked Aug 4, 2012 at 6:30 amravazziamravazzi 511 silver badge2 bronze badges 1
  • I MUST NOT use a script like this.... why? – JFK Commented Aug 4, 2012 at 8:19
Add a ment  | 

2 Answers 2

Reset to default 1

after the document ready, bind the button click event.

jQuery(document).ready(function(){

      $("#myLink").click(function() {
          $(this).fancybox({href : 'myPage.php'});
      });

 });

then there is no need for javascript code in your element.

<a href="#" id="myLink">Open ajax content</a>

Check this links for the jQuery click function and more about observer pattern

try

$("#myLink").click();

it should trigger the onclick event.

发布评论

评论列表(0)

  1. 暂无评论