2012-08-09 42 views
2

我试图以编程方式点击jQuery Mobile中的链接,但它不工作。这是页面代码。以编程方式点击jQuery Mobile应用程序中的链接

<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> 

,这里是JavaScript代码correpsonding:

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

似乎很简单,但我可以,不像是会使其work.Any帮助表示赞赏。

回答

0

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

$(document).ready(function() 
{ 
$("#launcher").bind("click",function() 
{ 
    console.log("Inside the button click handler"); 
    window.open($("#myLink").attr("href"),$("#myLink").attr("target")); 
}); 
}); 
1

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

0

你应该使用触发器,像这样:

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

还,请检查您的结合实际上是被绑定到一个元素。在JQM中,您不应该依赖$(document).ready。请使用$(document).bind('pageinit')。见here

0

我有同样的问题。 对我来说有效的是在DOM对象本身上调用click()。在jQuery对象后面添加“[0]”。

像这样:

$(document).ready(function() 
{ 
$("#launcher").bind("click",function() 
{ 
    console.log("Inside the button click handler"); 
    $("#myLink")[0].click(); 
}); 
}); 
相关问题