代码之家  ›  专栏  ›  技术社区  ›  tgandrews

click()可以在IE中使用,但不能在Firefox中使用。

  •  9
  • tgandrews  · 技术社区  · 16 年前

    我有一些简单的代码,但只适用于IE,而不是火狐。

    $(document).ready(function(){
        $('li#first').click();
    });
    

    我也尝试过:

    document.getElementById('first').click();
    

    但这也不管用。

    这是IE bug/功能还是 click() 其他浏览器不支持?

    回复意见:

    1. 有一个ID为的元素 首先,没有更多。
    2. 它是对list元素的一个onclick,用于扩展元素并将焦点移动到google map元素上。
    3. 在patrick的响应中运行代码(向元素添加另一个单击事件)产生了一些有趣的行为。跑步时 $('li#first').click() 仅激发新事件,但用鼠标物理单击元素同时激发(新事件和原始事件)。

    事先谢谢。

    5 回复  |  直到 14 年前
        1
  •  10
  •   tgandrews    16 年前

    火狐不支持click()。

    运行 document.getElementById('first').click() 返回以下错误 click is not a function

    所以我添加了一段代码来向每个元素添加click()功能。 This code 在一系列痛苦的谷歌搜索后发现 this thread .

    代码段如下,只需在页面上包含一次:

    HTMLElement.prototype.click = function() {
       var evt = this.ownerDocument.createEvent('MouseEvents');
       evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
       this.dispatchEvent(evt);
    } 
    
        2
  •  2
  •   Álvaro González    15 年前

    您的代码应该在Firefox中工作。下面是一个完整的片段来证明这一点:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title></title>
    <style type="text/css"><!--
    --></style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript"><!--
    jQuery(function($){
        $(document).ready(function(){
            $('li#first')
                .click(function(){
                    alert("I've been clicked: " + $(this).text());
                })
                .click();
        });
    });
    //--></script>
    </head>
    <body>
    
    <ul>
        <li id="first">First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>
    
    </body>
    </html>
    
        3
  •  1
  •   Abhidev    14 年前

    试试这个 HTML

    <a id="link" href="url">click here</a>
    

    JS

    $(document).ready(function(){
        $("#link").click(function(){
            window.location.href = $(this).attr('href');
        });
    });
    

    另外,如果这是你不想做的事情,也可以尝试这样做:

    $(document).ready(function(){
            $("#link")[0].click();
        });
    
        4
  •  0
  •   Xavi Esteve    16 年前

    你想转到第一个链接吗?不能通过单击触发链接的默认操作。

    但这里有一个解决方法:

    var link = jQuery("#first>a", this)
    if(!link.onclick) window.open(link.href, link.target || "_self")
    else jQuery(link).click()
    

    来源: http://forum.jquery.com/topic/jquery-a-0-click-not-working

        5
  •  0
  •   MZ0    14 年前

    firefox支持.click()jquery函数。我也有同样的问题,直到我在选择器中指定了标记名。基本上,我有这样的经历:

    <a id="vidLeftArrow" blah blah blah>...</a>
    $("#vidLeftArrow").click(function () {
       //Do something
    });
    

    这不管用。我必须将javascript更改为:

    <a id="vidLeftArrow" blah blah blah>...</a>
    $("a#vidLeftArrow").click(function () {
       //Do something
    });