如果我还没有用bind或click将事件处理程序绑定到一个链接上,我是否可以调用jquery click()来跟随它?

时间:2022-08-26 10:05:35

I have a timer in my JavaScript which needs to emulate clicking a link to go to another page once the time elapses. To do this I'm using jQuery's click() function. I have used $().trigger() and window.location also, and I can make it work as intended with all three.

我的JavaScript中有一个计时器,当时间过去时,它需要模拟单击一个链接到另一个页面。为此,我使用jQuery的click()函数。我使用了$().trigger()和窗口。还有位置,我可以让它在这三个地方都能正常工作。

I've observed some weird behavior with click() and I'm trying to understand what happens and why.

我在click()中观察到了一些奇怪的行为,我正在试图理解发生了什么以及原因。

I'm using Firefox for everything I describe in this question, but I am also interested in what other browsers will do with this.

我在这个问题中所描述的一切都使用Firefox,但我也对其他浏览器将如何使用它感兴趣。

If I have not used $('a').bind('click',fn) or $('a').click(fn) to set an event handler, then calling $('a').click() seems to do nothing at all. It does not call the browser's default handler for this event, as the browser does not load the new page.

如果我没有使用$('a').bind('click',fn)或$('a').click(fn)来设置事件处理程序,那么调用$('a').click()似乎什么都不做。它不会为这个事件调用浏览器的默认处理程序,因为浏览器不会加载新页面。

However, if I set an event handler first, then it works as expected, even if the event handler does nothing.

但是,如果我先设置一个事件处理程序,那么它就会按预期工作,即使事件处理程序什么也不做。

$('a').click(function(){return true;}).click();

This loads the new page as if I had clicked the a myself.

这将加载新页面,就好像我自己单击了a。

So my question is twofold: Is this weird behavior because I'm doing something wrong somewhere? and why does calling click() do nothing with the default behavior if I haven't created a handler of my own?

我的问题是双重的:这种奇怪的行为是因为我做错了什么吗?如果我没有创建自己的处理程序,为什么调用click()对默认行为不起作用呢?

EDIT:

编辑:

As Hoffman determined when he tried to duplicate my results, the outcome I described above doesn't actually happen. I'm not sure what caused the events I observed yesterday, but I'm certain today that it was not what I described in the question.

正如Hoffman所决定的,当他试图复制我的结果时,我上面描述的结果实际上并没有发生。我不确定是什么导致了我昨天观察到的事件,但我确信今天这不是我在问题中所描述的。

So the answer is that you can't "fake" clicks in the browser and that all jQuery does is call your event handler. You can still use window.location to change page, and that works fine for me.

所以答案是你不能在浏览器中“伪造”点击,jQuery只会调用事件处理器。你仍然可以使用窗口。位置更改页面,这对我来说没问题。

10 个解决方案

#1


85  

Interesting, this is probably a "feature request" (ie bug) for jQuery. The jQuery click event only triggers the click action (called onClick event on the DOM) on the element if you bind a jQuery event to the element. You should go to jQuery mailing lists ( http://forum.jquery.com/ ) and report this. This might be the wanted behavior, but I don't think so.

有趣的是,这可能是jQuery的“特性请求”(即bug)。如果您将jQuery事件绑定到元素,jQuery click事件只会触发元素的单击操作(在DOM上称为onClick事件)。您应该访问jQuery邮件列表(http://forum.jquery.com/)并报告它。这可能是想要的行为,但我不这么认为。

EDIT:

编辑:

I did some testing and what you said is wrong, even if you bind a function to an 'a' tag it still doesn't take you to the website specified by the href attribute. Try the following code:

我做了一些测试,你说的是错误的,即使你把一个函数绑定到一个'a'标签它仍然不会带你到href属性指定的网站。试试下面的代码:

<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
 <script>
  $(document).ready(function() {
   /* Try to dis-comment this:
   $('#a').click(function () {
    alert('jQuery.click()');
    return true;
   });
   */
  });
  function button_onClick() {
   $('#a').click();
  }
  function a_onClick() {
   alert('a_onClick');
  }
 </script>

</head>
<body>
 <input type="button" onclick="button_onClick()">
 <br>
 <a id='a' href='http://www.google.com' onClick="a_onClick()"> aaa </a>

</body>
</html> 

It never goes to google.com unless you directly click on the link (with or without the commented code). Also notice that even if you bind the click event to the link it still doesn't go purple once you click the button. It only goes purple if you click the link directly.

除非你直接点击链接(有或没有注释代码),否则它不会进入google。com。还要注意,即使您将单击事件绑定到链接,单击按钮后,它仍然不会变为紫色。如果你直接点击链接,它就会变成紫色。

I did some research and it seems that the .click is not suppose to work with 'a' tags because the browser does not suport "fake clicking" with javascript. I mean, you can't "click" an element with javascript. With 'a' tags you can trigger its onClick event but the link won't change colors (to the visited link color, the default is purple in most browsers). So it wouldn't make sense to make the $().click event work with 'a' tags since the act of going to the href attribute is not a part of the onClick event, but hardcoded in the browser.

我做了一些研究,似乎.click不应该使用“a”标签,因为浏览器不支持javascript的“假点击”。我的意思是,你不能用javascript“点击”一个元素。使用“a”标签,你可以触发它的onClick事件,但是链接不会改变颜色(在大多数浏览器中,默认是紫色的)。所以赚$()是没有意义的。使用“a”标签单击事件处理,因为进入href属性不是onClick事件的一部分,而是在浏览器中硬编码的。

#2


234  

Another option is of course to just use vanilla javascript :

另一个选择当然是使用普通的javascript:

document.getElementById("a_link").click()

#3


61  

If you look at the code for the $.click function I'll bet there is a conditional statement that checks to see if the element has listeners registered for theclick event before it proceeds. Why not just get the href attribute from the link and manually change the page location?

如果您查看$的代码。单击function,我敢打赌有一个条件语句,它检查元素是否在它开始之前已经为theclick事件注册了监听器。为什么不直接从链接中获取href属性并手动更改页面位置呢?

 window.location.href = $('a').attr('href');

EDIT: Here is why it doesn't click through, from the trigger function, jQuery source for version 1.3.2:

编辑:这里是为什么它不点击,从触发器函数,jQuery源代码版本1.3.2:

 // Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
    if ( (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
        event.result = false;

    // Trigger the native events (except for clicks on links)
    if ( !bubbling && elem[type] && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) {
        this.triggered = true;
        try {
            elem[ type ]();
        // prevent IE from throwing an error for some hidden elements
        } catch (e) {}
    }

After it calls handlers (if there are any) jQuery triggers an event on the object. However it only calls native handlers for click events if the element is not a link. I guess this was done purposefully for some reason. This should be true though whether an event handler is defined or not, so I'm not sure why in your case attaching an event handler caused the native onClick handler to be called. You'll have to do what I did and step through the execution to see where it is being called.

在调用了处理程序(如果有的话)之后,jQuery触发了对象上的一个事件。但是,如果元素不是链接,它只对单击事件调用本地处理程序。我猜这是故意的。不管是否定义了事件处理程序,这都应该是正确的,所以我不确定为什么在您的案例中附加一个事件处理程序会导致调用本机onClick处理程序。您将不得不执行我所做的,并逐步执行,以查看调用它的位置。

#4


5  

Click handlers on anchor tags are a special case in jQuery.

在jQuery中,锚标记上的单击处理程序是一种特殊情况。

I think you might be getting confused between the anchor's onclick event (known by the browser) and the click event of the jQuery object which wraps the DOM's notion of the anchor tag.

我认为您可能会混淆锚的onclick事件(浏览器知道)和jQuery对象的click事件,后者封装了DOM的锚标记概念。

You can download the jQuery 1.3.2 source here.

您可以在这里下载jQuery 1.3.2源代码。

The relevant sections of the source are lines 2643-2645 (I have split this out to multiple lines to make it easier to comprehend):

资料来源的相关部分是第2643-2645行(我将其分成多行,以便更容易理解):

// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
if (
     (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && 
       elem["on"+type] && 
       elem["on"+type].apply( elem, data ) === false
   )
     event.result = false;

#5


3  

JS/jQuery doesn't support the default behavior of links "clicked" programmatically.

JS/jQuery不支持以编程方式“单击”链接的默认行为。

What you can do is create a form and submit it. This way you don't have to use window.location or window.open, which are often blocked as unwanted popups by browsers.

您可以做的是创建一个表单并提交它。这样你就不用使用窗口了。位置或窗口。打开,通常被浏览器阻塞为不需要的弹出窗口。

This script has 2 different methods: one that tries to open 3 new tabs/windows (it opens only 1 in IE and Chrome, more info below) and one that fires a custom event on link click.

这个脚本有两种不同的方法:一种是尝试打开3个新的制表/窗口(它在IE和Chrome中只打开一个,下面有更多的信息),另一种是在点击链接时触发一个自定义事件。

Here is how:

这里是:

HTML

<html>
<head>
    <script src="jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="script.js" type="text/javascript"></script>
</head>
<body>
    <button id="testbtn">Test</button><br><br>

    <a href="https://google.nl">GOOGLE</a><br>
    <a href="http://en.wikipedia.org/wiki/Main_Page">WIKI</a><br>
    <a href="https://*.com/">SO</a>
</body>
</html>

jQuery (script.js)

$(function()
{ 
    // Try to open all 3 links by pressing the button
    // - Firefox opens all 3 links
    // - Chrome only opens 1 of them without popup warning
    // - IE only opens 1 of them WITH popup warning
    $("#testbtn").on("click", function()
    {
        $("a").each(function()
        {
            var form = $("<form></form>");
            form.attr(
            {
                id     : "formform",
                action : $(this).attr("href"),
                method : "GET",
                // Open in new window/tab
                target : "_blank"
            });

            $("body").append(form);
            $("#formform").submit();
            $("#formform").remove();
        });
    });

    // Or click the link and fire a custom event 
    // (open your own window without following the link itself)
    $("a").on("click", function()
    {
        var form = $("<form></form>");
        form.attr(
        {
            id     : "formform",
            // The location given in the link itself
            action : $(this).attr("href"), 
            method : "GET",
            // Open in new window/tab
            target : "_blank"              
        });

        $("body").append(form);
        $("#formform").submit();
        $("#formform").remove();

        // Prevent the link from opening normally
        return false;
    });

});

What it does is for each link element:

它所做的是为每个链接元素:

  1. Create a form
  2. 创建一个表单
  3. Give it attributes
  4. 给它的属性
  5. Append it to the DOM so it can be submitted
  6. 将其附加到DOM,以便提交
  7. Submit it
  8. 提交
  9. Remove the form from the DOM, removing all traces *Insert evil laugh*
  10. 从DOM中删除窗体,删除所有痕迹*插入邪恶的笑声*

Now you have a new tab/window loading "https://google.nl" (or any URL you want, just replace it). Unfortunately when you try to open more than one window this way, you get an Popup blocked messagebar when trying to open the second one (the first one is still opened).

现在你有了一个新的标签/窗口加载“https://google”。nl"(或任何你想要的URL,只需替换它)。不幸的是,当您尝试以这种方式打开多个窗口时,当您试图打开第二个窗口时(第一个窗口仍然被打开),您会得到一个弹出窗口阻塞的messagebar。


More info on how I got to this method is found here:

更多关于我如何得到这个方法的信息可以在这里找到:

Opening new window/tab without using window.open or window.location.href

不使用窗口打开新的窗口/选项卡。打开或window.location.href

#6


3  

Trigger a Hyperlink element that is inside the element you want to hookup the jquery .click()

触发要连接jquery .click()的元素中的超链接元素

<div class="TopicControl">
    <div class="articleImage">
       <a href=""><img src="" alt=""></a>
    </div>
</div>

In your script you hookup to the main container you want the click event on. Then you use standard jquery methodology to find the element (type,class,id) and fire the click. What happens is jquery enters a recursive function to fire the click and you break the recursive function by takeing the event 'e' and stopPropagation() function and return false because you don't want jquery to do anything else but fire the link.

在您的脚本中,您将连接到要打开单击事件的主容器。然后使用标准的jquery方法查找元素(类型、类、id)并单击。jquery进入了一个递归函数来触发点击,通过调用事件“e”和stopPropagation()函数来破坏递归函数并返回false,因为您不希望jquery除了触发链接之外做任何事情。

$('.TopicControl').click(function (event) {
         $(this).find('a').click();
        event.stopPropagation();
        return false;
     });

Alternative solution is to wrap the containers in the element and place 's as containers inside instead of 's. Set the spans to display block to conform with w3c standards.

另一种解决方案是将容器包装在元素中,将容器作为容器放置在元素中而不是容器中。

#7


2  

It does nothing because no events have been bound to the event. If I recall correctly, jQuery maintains its own list of event handlers that are bound to NodeLists for performance and other purposes.

它什么也不做,因为没有事件被绑定到事件。如果我没记错的话,jQuery维护了它自己的事件处理程序列表,这些事件处理程序为性能和其他目的绑定到节点列表。

#8


2  

If you need this feature for one case or very few casses. (you'r whole app is not requiring this feature) I would rather leave jQuery as is (for many reasons, including being able to update to newer versions, CDN etc.) and have the following workaround:

如果您需要在一个案例或很少的案例中使用此特性。(你的整个应用不需要这个特性)我宁愿让jQuery保持原样(因为很多原因,包括能够更新到更新的版本,CDN等等),并有以下的解决方案:

// For modren Browsers
$(ele).trigger("click");

// Relaying on Paul Irish's conditional class names http://bit.ly/HWIpAp (via HTML5 Boilerplate http://bit.ly/HUzi3I) where each IE version gets a class of its Version
$("html.ie7").length && (function(){
    var eleOnClickattr = $(ele).attr("onclick") 
    eval(eleOnClickattr);
  })()

#9


1  

You can use jQuery to select the jQuery object for that element. Then, get the underlying DOM element and call its click() method.

您可以使用jQuery为该元素选择jQuery对象。然后,获取底层DOM元素并调用它的click()方法。

by id

通过id

$("#my-link").each(function (index) { $(this).get(0).click() });

or use jQuery to click a bunch of links by CSS class

或者使用jQuery点击CSS类的链接。

$(".my-link-class").each(function (index) { $(this).get(0).click() });

#10


0  

To open hyperlink in same tab, use:

若要在同一选项卡中打开超链接,请使用:

$(document).on('click', "a.classname", function() {
    var form = $("<form></form>");
    form.attr(
    {
        id     : "formid",
        action : $(this).attr("href"),
        method : "GET",
    });

    $("body").append(form);
    $("#formid").submit();
    $("#formid").remove();
    return false;
});

#1


85  

Interesting, this is probably a "feature request" (ie bug) for jQuery. The jQuery click event only triggers the click action (called onClick event on the DOM) on the element if you bind a jQuery event to the element. You should go to jQuery mailing lists ( http://forum.jquery.com/ ) and report this. This might be the wanted behavior, but I don't think so.

有趣的是,这可能是jQuery的“特性请求”(即bug)。如果您将jQuery事件绑定到元素,jQuery click事件只会触发元素的单击操作(在DOM上称为onClick事件)。您应该访问jQuery邮件列表(http://forum.jquery.com/)并报告它。这可能是想要的行为,但我不这么认为。

EDIT:

编辑:

I did some testing and what you said is wrong, even if you bind a function to an 'a' tag it still doesn't take you to the website specified by the href attribute. Try the following code:

我做了一些测试,你说的是错误的,即使你把一个函数绑定到一个'a'标签它仍然不会带你到href属性指定的网站。试试下面的代码:

<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
 <script>
  $(document).ready(function() {
   /* Try to dis-comment this:
   $('#a').click(function () {
    alert('jQuery.click()');
    return true;
   });
   */
  });
  function button_onClick() {
   $('#a').click();
  }
  function a_onClick() {
   alert('a_onClick');
  }
 </script>

</head>
<body>
 <input type="button" onclick="button_onClick()">
 <br>
 <a id='a' href='http://www.google.com' onClick="a_onClick()"> aaa </a>

</body>
</html> 

It never goes to google.com unless you directly click on the link (with or without the commented code). Also notice that even if you bind the click event to the link it still doesn't go purple once you click the button. It only goes purple if you click the link directly.

除非你直接点击链接(有或没有注释代码),否则它不会进入google。com。还要注意,即使您将单击事件绑定到链接,单击按钮后,它仍然不会变为紫色。如果你直接点击链接,它就会变成紫色。

I did some research and it seems that the .click is not suppose to work with 'a' tags because the browser does not suport "fake clicking" with javascript. I mean, you can't "click" an element with javascript. With 'a' tags you can trigger its onClick event but the link won't change colors (to the visited link color, the default is purple in most browsers). So it wouldn't make sense to make the $().click event work with 'a' tags since the act of going to the href attribute is not a part of the onClick event, but hardcoded in the browser.

我做了一些研究,似乎.click不应该使用“a”标签,因为浏览器不支持javascript的“假点击”。我的意思是,你不能用javascript“点击”一个元素。使用“a”标签,你可以触发它的onClick事件,但是链接不会改变颜色(在大多数浏览器中,默认是紫色的)。所以赚$()是没有意义的。使用“a”标签单击事件处理,因为进入href属性不是onClick事件的一部分,而是在浏览器中硬编码的。

#2


234  

Another option is of course to just use vanilla javascript :

另一个选择当然是使用普通的javascript:

document.getElementById("a_link").click()

#3


61  

If you look at the code for the $.click function I'll bet there is a conditional statement that checks to see if the element has listeners registered for theclick event before it proceeds. Why not just get the href attribute from the link and manually change the page location?

如果您查看$的代码。单击function,我敢打赌有一个条件语句,它检查元素是否在它开始之前已经为theclick事件注册了监听器。为什么不直接从链接中获取href属性并手动更改页面位置呢?

 window.location.href = $('a').attr('href');

EDIT: Here is why it doesn't click through, from the trigger function, jQuery source for version 1.3.2:

编辑:这里是为什么它不点击,从触发器函数,jQuery源代码版本1.3.2:

 // Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
    if ( (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
        event.result = false;

    // Trigger the native events (except for clicks on links)
    if ( !bubbling && elem[type] && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) {
        this.triggered = true;
        try {
            elem[ type ]();
        // prevent IE from throwing an error for some hidden elements
        } catch (e) {}
    }

After it calls handlers (if there are any) jQuery triggers an event on the object. However it only calls native handlers for click events if the element is not a link. I guess this was done purposefully for some reason. This should be true though whether an event handler is defined or not, so I'm not sure why in your case attaching an event handler caused the native onClick handler to be called. You'll have to do what I did and step through the execution to see where it is being called.

在调用了处理程序(如果有的话)之后,jQuery触发了对象上的一个事件。但是,如果元素不是链接,它只对单击事件调用本地处理程序。我猜这是故意的。不管是否定义了事件处理程序,这都应该是正确的,所以我不确定为什么在您的案例中附加一个事件处理程序会导致调用本机onClick处理程序。您将不得不执行我所做的,并逐步执行,以查看调用它的位置。

#4


5  

Click handlers on anchor tags are a special case in jQuery.

在jQuery中,锚标记上的单击处理程序是一种特殊情况。

I think you might be getting confused between the anchor's onclick event (known by the browser) and the click event of the jQuery object which wraps the DOM's notion of the anchor tag.

我认为您可能会混淆锚的onclick事件(浏览器知道)和jQuery对象的click事件,后者封装了DOM的锚标记概念。

You can download the jQuery 1.3.2 source here.

您可以在这里下载jQuery 1.3.2源代码。

The relevant sections of the source are lines 2643-2645 (I have split this out to multiple lines to make it easier to comprehend):

资料来源的相关部分是第2643-2645行(我将其分成多行,以便更容易理解):

// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
if (
     (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && 
       elem["on"+type] && 
       elem["on"+type].apply( elem, data ) === false
   )
     event.result = false;

#5


3  

JS/jQuery doesn't support the default behavior of links "clicked" programmatically.

JS/jQuery不支持以编程方式“单击”链接的默认行为。

What you can do is create a form and submit it. This way you don't have to use window.location or window.open, which are often blocked as unwanted popups by browsers.

您可以做的是创建一个表单并提交它。这样你就不用使用窗口了。位置或窗口。打开,通常被浏览器阻塞为不需要的弹出窗口。

This script has 2 different methods: one that tries to open 3 new tabs/windows (it opens only 1 in IE and Chrome, more info below) and one that fires a custom event on link click.

这个脚本有两种不同的方法:一种是尝试打开3个新的制表/窗口(它在IE和Chrome中只打开一个,下面有更多的信息),另一种是在点击链接时触发一个自定义事件。

Here is how:

这里是:

HTML

<html>
<head>
    <script src="jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="script.js" type="text/javascript"></script>
</head>
<body>
    <button id="testbtn">Test</button><br><br>

    <a href="https://google.nl">GOOGLE</a><br>
    <a href="http://en.wikipedia.org/wiki/Main_Page">WIKI</a><br>
    <a href="https://*.com/">SO</a>
</body>
</html>

jQuery (script.js)

$(function()
{ 
    // Try to open all 3 links by pressing the button
    // - Firefox opens all 3 links
    // - Chrome only opens 1 of them without popup warning
    // - IE only opens 1 of them WITH popup warning
    $("#testbtn").on("click", function()
    {
        $("a").each(function()
        {
            var form = $("<form></form>");
            form.attr(
            {
                id     : "formform",
                action : $(this).attr("href"),
                method : "GET",
                // Open in new window/tab
                target : "_blank"
            });

            $("body").append(form);
            $("#formform").submit();
            $("#formform").remove();
        });
    });

    // Or click the link and fire a custom event 
    // (open your own window without following the link itself)
    $("a").on("click", function()
    {
        var form = $("<form></form>");
        form.attr(
        {
            id     : "formform",
            // The location given in the link itself
            action : $(this).attr("href"), 
            method : "GET",
            // Open in new window/tab
            target : "_blank"              
        });

        $("body").append(form);
        $("#formform").submit();
        $("#formform").remove();

        // Prevent the link from opening normally
        return false;
    });

});

What it does is for each link element:

它所做的是为每个链接元素:

  1. Create a form
  2. 创建一个表单
  3. Give it attributes
  4. 给它的属性
  5. Append it to the DOM so it can be submitted
  6. 将其附加到DOM,以便提交
  7. Submit it
  8. 提交
  9. Remove the form from the DOM, removing all traces *Insert evil laugh*
  10. 从DOM中删除窗体,删除所有痕迹*插入邪恶的笑声*

Now you have a new tab/window loading "https://google.nl" (or any URL you want, just replace it). Unfortunately when you try to open more than one window this way, you get an Popup blocked messagebar when trying to open the second one (the first one is still opened).

现在你有了一个新的标签/窗口加载“https://google”。nl"(或任何你想要的URL,只需替换它)。不幸的是,当您尝试以这种方式打开多个窗口时,当您试图打开第二个窗口时(第一个窗口仍然被打开),您会得到一个弹出窗口阻塞的messagebar。


More info on how I got to this method is found here:

更多关于我如何得到这个方法的信息可以在这里找到:

Opening new window/tab without using window.open or window.location.href

不使用窗口打开新的窗口/选项卡。打开或window.location.href

#6


3  

Trigger a Hyperlink element that is inside the element you want to hookup the jquery .click()

触发要连接jquery .click()的元素中的超链接元素

<div class="TopicControl">
    <div class="articleImage">
       <a href=""><img src="" alt=""></a>
    </div>
</div>

In your script you hookup to the main container you want the click event on. Then you use standard jquery methodology to find the element (type,class,id) and fire the click. What happens is jquery enters a recursive function to fire the click and you break the recursive function by takeing the event 'e' and stopPropagation() function and return false because you don't want jquery to do anything else but fire the link.

在您的脚本中,您将连接到要打开单击事件的主容器。然后使用标准的jquery方法查找元素(类型、类、id)并单击。jquery进入了一个递归函数来触发点击,通过调用事件“e”和stopPropagation()函数来破坏递归函数并返回false,因为您不希望jquery除了触发链接之外做任何事情。

$('.TopicControl').click(function (event) {
         $(this).find('a').click();
        event.stopPropagation();
        return false;
     });

Alternative solution is to wrap the containers in the element and place 's as containers inside instead of 's. Set the spans to display block to conform with w3c standards.

另一种解决方案是将容器包装在元素中,将容器作为容器放置在元素中而不是容器中。

#7


2  

It does nothing because no events have been bound to the event. If I recall correctly, jQuery maintains its own list of event handlers that are bound to NodeLists for performance and other purposes.

它什么也不做,因为没有事件被绑定到事件。如果我没记错的话,jQuery维护了它自己的事件处理程序列表,这些事件处理程序为性能和其他目的绑定到节点列表。

#8


2  

If you need this feature for one case or very few casses. (you'r whole app is not requiring this feature) I would rather leave jQuery as is (for many reasons, including being able to update to newer versions, CDN etc.) and have the following workaround:

如果您需要在一个案例或很少的案例中使用此特性。(你的整个应用不需要这个特性)我宁愿让jQuery保持原样(因为很多原因,包括能够更新到更新的版本,CDN等等),并有以下的解决方案:

// For modren Browsers
$(ele).trigger("click");

// Relaying on Paul Irish's conditional class names http://bit.ly/HWIpAp (via HTML5 Boilerplate http://bit.ly/HUzi3I) where each IE version gets a class of its Version
$("html.ie7").length && (function(){
    var eleOnClickattr = $(ele).attr("onclick") 
    eval(eleOnClickattr);
  })()

#9


1  

You can use jQuery to select the jQuery object for that element. Then, get the underlying DOM element and call its click() method.

您可以使用jQuery为该元素选择jQuery对象。然后,获取底层DOM元素并调用它的click()方法。

by id

通过id

$("#my-link").each(function (index) { $(this).get(0).click() });

or use jQuery to click a bunch of links by CSS class

或者使用jQuery点击CSS类的链接。

$(".my-link-class").each(function (index) { $(this).get(0).click() });

#10


0  

To open hyperlink in same tab, use:

若要在同一选项卡中打开超链接,请使用:

$(document).on('click', "a.classname", function() {
    var form = $("<form></form>");
    form.attr(
    {
        id     : "formid",
        action : $(this).attr("href"),
        method : "GET",
    });

    $("body").append(form);
    $("#formid").submit();
    $("#formid").remove();
    return false;
});