click()(javascript)方法在FF中不起作用 [英] click() (javascript) method is not working in FF
问题描述
以下代码在IE中按预期抛出两个警报但在Firefox中没有。请帮忙。
The following code is throwing two alerts as expected in IE but not in Firefox. Please help.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function myFunction(){
alert('myfunc');
document.getElementById('mylabel').click();
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<p id='mylabel' onclick="alert('you reached');"></p>
<input type='button' value="Click me" onclick='myFunction();'/>
</BODY>
</HTML>
谢谢
推荐答案
Firefox只有一个click()函数用于表单元素,如按钮。但是,您可以直接调用onClick函数;您可以将行更改为
Firefox only has a click() function for form elements such as buttons. However, you can call the onClick function directly; you can change the line to
document.getElementById('mylabel').onclick();
这适用于firefox或IE(但请注意,它要求该功能实际存在,你知道在这个例子中它确实如此)。
This works in firefox or IE (but note that it requires that the function actually exists, which you know it does in this example).
还要注意你实际上并没有模拟对该元素的点击(例如,如果有其他东西这样的话点击会做,比如也可以作为点击容器,它们不会发生)。你只是获得了一个点击运行的功能,并直接运行它。因此,对于您需要模拟点击的所有情况,它都不是解决方案。
Also note that you aren't actually simulating a click on that element (so, for example, if there were other things that such a click would do, such as also act as a click on the container, they won't happen). You're just getting the function that would run on a click, and running it directly. So it's not a solution for all situations where you need to simulate a click.
这篇关于click()(javascript)方法在FF中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!