javascript 在页面加载时使用 jQuery 以编程方式单击链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13041161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:42:53  来源:igfitidea点击:

Programmatically click on a link using jQuery on page load

javascriptjquery

提问by Claudio Delgado

I have a link that is on the page and I want to click it programmatically when the page loads.

我在页面上有一个链接,我想在页面加载时以编程方式单击它。

Something like this:

像这样的东西:

$(document).ready(function () {

   // code to make the page think $('a.tagcloud-link') is clicked
});

The link is an AJAX call that displays the popular tags in Wordpress backend.

该链接是一个 AJAX 调用,用于显示 Wordpress 后端中的流行标签。

The function cannot easily be edited as it's dependent on many things.

该函数不能轻易编辑,因为它依赖于很多东西。

So I want to trigger the AJAX link that calls the most popular tags on page load.

所以我想触发在页面加载时调用最流行标签的 AJAX 链接。

This will solve all my problems if it works.

如果它有效,这将解决我所有的问题。

That link is $('a.tagcloud-link')

该链接是 $('a.tagcloud-link')

Please, please help me on this I've spent hours pulling my hair out trying and failing to do this. Thank you.

拜托,请帮我解决这个问题,我花了好几个小时把头发拉出来,试图做到这一点却没有做到。谢谢你。

采纳答案by Claudio Delgado

None of these helped. I fixed it myself by bypassing the link and executing it on load.

这些都没有帮助。我通过绕过链接并在加载时执行它自己修复了它。

回答by nnnnnn

Try this:

试试这个:

$('a.tagcloud-link')[0].click();

Using [0]gets a reference to the first DOM object in the jQuery object so this calls the DOM object's (non-jQuery) .click()methodwhich will cause the navigation to the link's specified href.

Using[0]获取对 jQuery 对象中第一个 DOM 对象的引用,因此这将调用DOM 对象的(非 jQuery).click()方法,这将导致导航到链接的指定href.

Using jQuery's .click()methodwould call any click handler that you'd bound to the element, but won't cause the default behaviour to actually follow the link.

使用jQuery 的.click()方法将调用您绑定到元素的任何点击处理程序,但不会导致默认行为实际跟随链接。

Reallysimple demo: http://jsfiddle.net/mtBav/

非常简单的演示:http: //jsfiddle.net/mtBav/

回答by jAndy

If that element owns an event handlerwhich was bound with jQuerytoo, you might just call .trigger( 'click' )or short .click();

如果该元素也拥有一个与jQuery绑定的事件处理程序,您可能只需调用或 short ;.trigger( 'click' ).click()

$(function() {
    $('a.tagcloud-link').click();
});

Of course, at the time when you invoke .click(), that event handler must be bound already.

当然,在您调用 时.click(),必须已经绑定了该事件处理程序。

回答by Heart

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a#tobeclicked").click(function () {
// if it has to be href
var tohref = $(this).attr("href");
alert(tohref);
window.location=tohref;


// else you want add some functions

$("#mydiv").fadeIn();

});
$("a#tobeclicked").trigger('click');
});
</script>
<a id="tobeclicked" href="http://www.google.com">Go!</a>
<div id="mydiv" style="display:none;">Stroam</div>

回答by Jonas T

If you have already binded $('a.tagcloud-link').click(), it will call that function. The following code should work.

如果您已经绑定了 $('a.tagcloud-link').click(),它将调用该函数。以下代码应该可以工作。

$(document).ready(function () {
 $('a.tagcloud-link').click();
});

But if you want to open the link in its href, you need to use window.location.

但是如果你想在它的 href 中打开链接,你需要使用window.location.

回答by Sushanth --

Try

尝试

$('a.tagcloud-link').click();

OR

或者

$('a.tagcloud-link').trigger('click')

回答by Angelo A

$(function() {
     $('a.tagcloud-link').on('click', function() {
          // execute code here
     });
});

This is proper use of jQuery.

这是 jQuery 的正确使用。

回答by Reflective

If I correctly understood the question:

如果我正确理解了这个问题:

   $('a.tagcloud-link').click();

回答by Jim

To be honest with you, I think everyone else here is right and I think the question is framed wrong. But heres my attempt and I think all I did was just frame it a little different. Just trying to help.

老实说,我认为这里的其他人都是对的,我认为这个问题的框架是错误的。但这是我的尝试,我认为我所做的只是将其框架略有不同。只是想帮忙。

document.addEventListener("DOMContentLoaded", function() {
    EventL();
});

// global
var tagLoad = false;

function eventL() {
    $('a.tagcloud-link').on('click', function() {
       tagLoad = true;
    });

    // you can repurpose this, its for page refresh right now. but
    // I think it helps with your general idea?
    $(window).load(function(){ 
       init();
    });
}

function init () {
    if (tagLoad == true) {
         // Load your tags.
    }
}

回答by fishpie

function my_admin_head(){
echo 
'<script type="text/javascript">
    jQuery(function( $ ){ 
        $(\'a.tagcloud-link\').each( function(){
            tagBox.get( $(this).attr(\'id\') );
            $(this).unbind().click(function(){
                $(this).siblings(\'.the-tagcloud\').toggle();
                return false;
            });
        return false;
    });
});
</script>';
}
add_action('admin_head', 'my_admin_head');

Maybe it can help somebody.

也许它可以帮助某人。