简体   繁体   English

单击页面加载时使用jQuery的链接

[英]click on a link using jQuery on page load

I have a link with a script in that opens a view of products. 我有一个带有脚本的链接,可以打开产品视图。 I don't have access to the html, and I need to have this view open on default. 我无权访问html,因此我需要默认打开此视图。 Until the behavior is fixed the correct way, which will take long time unfortunaly, I want to automatically make the link click on pageload. 在以正确的方式修复行为之前,这将花费很长时间,而我要自动使链接单击Pageload。

I tried with the script below first - but since the link itself triggers a pageload, this obviously just keeps firing over and over again. 我首先尝试使用下面的脚本-但由于链接本身触发了页面加载,因此很显然,这会不断触发。

So how do I do this with jquery or vaniall JS, is this even possible? 那么我该如何使用jquery或vaniall JS做到这一点呢?

Script: 脚本:

$(document).ready(function () {
  $('.action')[0].click();
});

My link: 我的连结:

<a href="javascript://" name="_ec_rpd1" id="_ec_rpd1" class="action" onclick="if( UI.pb_boolean(this, 'click') ) {} return false;">Toon alle</a>
$(document).ready(function () {
  $('.action:first').trigger("click");
});

to manually trigger an event u need to use trigger() , click is to only assign a handler 手动触发一个事件,您需要使用trigger()click仅分配一个处理程序

$(document).ready(function () {

  $('a.action').click(function(){
   ... handler code
});

  $('a.action').trigger("click");

});

Can you tell me why you add [0], if you need to add it for first element then use following code :- 您能告诉我为什么要添加[0]吗,如果需要将其添加到第一个元素,请使用以下代码:-

$(document).ready(function () {
  $('.action:first').trigger("click");
});

otherwise you can directly use these :- 否则,您可以直接使用这些:

$(document).ready(function () {
  $('.action').trigger("click");
});

Change your link tag with following :- 通过以下更改链接标签:-

<a href="javascript:void(0)" name="_ec_rpd1" id="_ec_rpd1" class="action" onclick="if( UI.pb_boolean(this, 'click') ) {} return false;">Toon alle</a>

The click() method triggers the click event. click()方法触发click事件。

Syntax : 句法 :

Trigger the click event for the selected elements: 触发所选元素的点击事件:

$(selector).click();

http://www.w3schools.com/jquery/event_click.asp ( example ) http://www.w3schools.com/jquery/event_click.asp示例

In your case : 在您的情况下:

 $(document).ready(function() { $('.action:first').click(); }); 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <a href="javascript://" name="_ec_rpd1" id="_ec_rpd1" class="action" onclick="if( UI.pb_boolean(this, 'click') ) {} return false;">Toon alle</a> 

Edit : If you don't want to trigger in a specific page. 编辑:如果您不想在特定页面中触发。 do like this. 做到这一点。

$(document).ready(function () {
    if (!location.href.indexOf("someurl") > -1)
        $('.action:first').click();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM