2

I am trying to programmatically click a link in jQuery Mobile but its not working. Here is the page code.

<section id="welcome" data-role="page">

<div class="content" data-role="content">
<a id="myLink"  href="http://www.google.de" target="_blank">The link</a>
<input type="button" id="launcher" value="Launch the link"/>
</div>
</section>

and here is the correpsonding javascript code:

$(document).ready(function()
{
$("#launcher").bind("click",function()
{
    console.log("Inside the button click handler");
    $("#myLink").click();
});
});

Seems straightforward enough but I can,t seem to make it work.Any help is appreciated.

4

4 回答 4

1

它不起作用的一个可能原因是<a id="myLink"></a>DOM 中有另一个,但在活动页面中没有。如果是这种情况,您的代码可能会触发该元素上的点击处理程序,而不是您在屏幕上看到的那个。也许使用$.mobile.activePage.find("#myLink").click();会起作用。

此外,您应该避免在 jQuery Mobile 中使用 $(document).ready()

于 2012-08-09T17:15:59.947 回答
0

您应该使用触发器,如下所示:

$('#mylink').trigger('click');

另外,请检查您的绑定实际上是否绑定到一个元素。在 JQM 中,您不应该依赖$(document).ready. 而是使用$(document).bind('pageinit'). 看这里

于 2012-08-10T14:49:13.673 回答
0

我有同样的问题。对我有用的是在 DOM 对象本身上调用 click()。只需在 jQuery 对象后添加“[0]”即可。

像这样:

$(document).ready(function()
{
$("#launcher").bind("click",function()
{
    console.log("Inside the button click handler");
    $("#myLink")[0].click();
});
});
于 2015-03-31T13:01:29.063 回答
0

不是问题的真正答案,但如果您只想实现要启动的链接(并且您不想执行您为链接定义的点击处理程序,如果有的话),这可能是一个解决方案

$(document).ready(function()
{
$("#launcher").bind("click",function()
{
    console.log("Inside the button click handler");
    window.open($("#myLink").attr("href"),$("#myLink").attr("target"));
});
});
于 2012-08-09T16:36:55.950 回答