我怎样才能模拟通过jQuery的锚点击? [英] How can I simulate an anchor click via jquery?

查看:87
本文介绍了我怎样才能模拟通过jQuery的锚点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过jQuery伪造锚点的问题:
为什么我第一次点击输入按钮时出现密码箱,而不是第二次或第三次?

以下是我的代码:

 < input onclick =$('#thickboxId')。点击(); type =buttonvalue =点击我/> 

< a id =thickboxIdhref =myScript.phpclass =thickboxtitle =>连结< / a>

当我直接点击链接时,它总是有效,但如果我尝试激活thickbox通过输入按钮。这是FF。对于Chrome,它似乎每次都有效。任何提示?

解决方案

尽量避免内联你的jQuery调用。将页面顶部的脚本标记绑定到点击事件:

 < script type =text / javascript> 
$(function(){
$('#thickboxButton')。click(function(){
$('#thickboxId')。click();
}) ;
});
< / script>

< input id =thickboxButtontype =buttonvalue =Click me>
< a id =thickboxIdhref =myScript.phpclass =thickboxtitle =>连结< / a>

编辑:

如果您试图模拟用户点击链接,那么我认为这是不可能的。解决方法是更新按钮的点击事件以更改Javascript中的 window.location

 < script type =text / javascript> 
$(function(){
$('#thickboxButton')。click(function(){
window.location = $('#thickboxId')。attr('href') ;
});
});
< / script>

编辑2:

现在我意识到Thickbox是一个自定义的jQuery UI小部件,我发现此处的说明:

使用说明:




  • 创建一个链接元素(< a href>

  • 给链接一个class属性,其值为thickbox( class =thickbox
  • 在链接的 href 属性中添加以下锚点: #TB_inline

  • #TB_inline 之后的 href code>将以下查询字符串添加到锚点:



    ?height = 300& width = 300& inlineId = myOnPageContent


  • 相应地更改查询中height,width和inlineId的值(inlineID是包含要在ThickBox中显示的内容的元素的ID值。


  • 可选您可以将modal = true添加到查询字符串(例如, #TB_inline?height = 155& width = 300& inlineId = hiddenModalContent& modal = true ),因此关闭ThickBox需要调用 tb_remove在ThickBox中功能。请参阅隐藏的模式内容示例,您必须单击是或否来关闭ThickBox。


I have a problem with faking an anchor click via jQuery: Why does my thickbox appear the first time I click on the input button, but not the second or third time?

Here is my code:

<input onclick="$('#thickboxId').click();" type="button" value="Click me" />

<a id="thickboxId" href="myScript.php" class="thickbox" title="">Link</a>

It does always work when I click directly on the link, but not if I try to activate the thickbox via the input button. This is in FF. For Chrome it seems to work every time. Any hints?

解决方案

Try to avoid inlining your jQuery calls like that. Put a script tag at the top of the page to bind to the click event:

<script type="text/javascript">
$(function(){
    $('#thickboxButton').click(function(){
        $('#thickboxId').click();
    });
});
</script>

<input id="thickboxButton" type="button" value="Click me">
<a id="thickboxId" href="myScript.php" class="thickbox" title="">Link</a>

Edit:

If you're trying to simulate a user physically clicking the link, then I don't believe that is possible. A workaround would be to update the button's click event to change the window.location in Javascript:

<script type="text/javascript">
$(function(){
    $('#thickboxButton').click(function(){
        window.location = $('#thickboxId').attr('href');
    });
});
</script>

Edit 2:

Now that I realize that Thickbox is a custom jQuery UI widget, I found the instructions here:

Instructions:

  • Create a link element (<a href>)
  • Give the link a class attribute with a value of thickbox (class="thickbox")
  • In the href attribute of the link add the following anchor: #TB_inline
  • In the href attribute after the #TB_inline add the following query string on to the anchor:

    ?height=300&width=300&inlineId=myOnPageContent

  • Change the values of height, width, and inlineId in the query accordingly (inlineID is the ID value of the element that contains the content you would like to show in a ThickBox.

  • Optionally you may add modal=true to the query string (e.g. #TB_inline?height=155&width=300&inlineId=hiddenModalContent&modal=true) so that closing a ThickBox will require calling the tb_remove() function from within the ThickBox. See the hidden modal content example, where you must click yes or no to close the ThickBox.

这篇关于我怎样才能模拟通过jQuery的锚点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆