5

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.

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.

This will solve all my problems if it works.

That link is $('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.

4

11 回答 11

7

尝试这个:

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

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

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

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

于 2012-10-24T00:19:31.257 回答
4

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

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

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

于 2012-10-24T00:10:11.937 回答
3
<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>
于 2012-10-24T04:32:03.273 回答
1

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

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.
    }
}
于 2012-10-24T00:51:55.677 回答
1

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

   $('a.tagcloud-link').click();
于 2012-10-24T00:09:44.780 回答
1

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

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

但是如果要在其 href 中打开链接,则需要使用window.location.

于 2012-10-24T00:10:26.260 回答
1

尝试

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

或者

$('a.tagcloud-link').trigger('click')
于 2012-10-24T00:10:32.847 回答
1
$(function() {
     $('a.tagcloud-link').on('click', function() {
          // execute code here
     });
});

这是对 jQuery 的正确使用。

于 2012-10-24T00:11:24.790 回答
0
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');

也许它可以帮助某人。

于 2014-10-16T09:09:39.037 回答
0

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

于 2012-10-25T00:05:17.463 回答
0

也许您用来访问元素的方式是将 jquery 语法与 javascript 混合。我也遇到了这个问题,但是我试图以错误的方式访问元素。

就像这样

$(document).ready(function(e){
        alert(location.hash);
        if (location.hash == "#22") {
            $("gallery div a")[1].click(); //without #
        };
    })

但应该是这样的

$(document).ready(function(e){
        alert(location.hash);
        if (location.hash == "#22") {
            $("#gallery div a")[1].click(); //with #, cause I was using an ID
        };
    })

并且有效:)

于 2016-09-09T01:16:20.550 回答