Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery触发器不工作 测试触发器 $(document).ready(函数(){//on document ready $(“#测试床”).trigger('click');//单击元素 })_Javascript_Jquery - Fatal编程技术网

Javascript Jquery触发器不工作 测试触发器 $(document).ready(函数(){//on document ready $(“#测试床”).trigger('click');//单击元素 })

Javascript Jquery触发器不工作 测试触发器 $(document).ready(函数(){//on document ready $(“#测试床”).trigger('click');//单击元素 }),javascript,jquery,Javascript,Jquery,我希望这段代码在文档准备就绪时自动单击testbed链接。但是链接没有被点击。为什么不试试这个 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </scr

我希望这段代码在文档准备就绪时自动单击testbed链接。但是链接没有被点击。

为什么不试试这个

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  </script>
  <title>test trigger</title>
</head>
<body>

  <script>
  $(document).ready(function(){ // on document ready
    $("#testbed").trigger('click'); // click the element
  })
  </script>

  <a id="testbed" href="http://www.google.com">testing</a>
</body>
</html>

$(document).ready(函数(){//on document ready
变量url=”http://www.google.com";
window.location.href=url;
})
为什么不试试这个

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  </script>
  <title>test trigger</title>
</head>
<body>

  <script>
  $(document).ready(function(){ // on document ready
    $("#testbed").trigger('click'); // click the element
  })
  </script>

  <a id="testbed" href="http://www.google.com">testing</a>
</body>
</html>

$(document).ready(函数(){//on document ready
变量url=”http://www.google.com";
window.location.href=url;
})

您需要定义点击
测试床

<script>
$(document).ready(function(){ // on document ready
    var url = "http://www.google.com";
    window.location.href = url;
})
</script>
如果您不想点击事件,只想导航到链接的href,那么您可以使用
window.location.href=url

$(document).ready(function(){ // on document ready    
    $("#testbed").click(function(){ // on document ready
       alert("clicked");
    });
    $("#testbed").trigger('click'); // click the element
});

尽可能使用本机函数和属性等来获得性能。您可以检查不同属性访问方法的性能差异

您需要定义点击
测试床

<script>
$(document).ready(function(){ // on document ready
    var url = "http://www.google.com";
    window.location.href = url;
})
</script>
如果您不想点击事件,只想导航到链接的href,那么您可以使用
window.location.href=url

$(document).ready(function(){ // on document ready    
    $("#testbed").click(function(){ // on document ready
       alert("clicked");
    });
    $("#testbed").trigger('click'); // click the element
});

尽可能使用本机函数和属性等来获得性能。您可以检查不同属性访问方法的性能差异

触发器用于执行链接测试台没有的单击事件。如果要将页面重定向到该链接,为什么不使用window.location.href

$(document).ready(function(){ // on document ready       
    window.location.href = document.getElementById("testbed").href;
});

触发器用于执行链接测试台没有的单击事件。如果要将页面重定向到该链接,为什么不使用window.location.href

$(document).ready(function(){ // on document ready       
    window.location.href = document.getElementById("testbed").href;
});

将触发
单击
事件。例如:

window.location.href = $("#testbed").attr('href');

href
中访问URL的实际操作不会发生。您可以使用
window.location
更改页面并绕过jQuery。

触发
单击事件。例如:

window.location.href = $("#testbed").attr('href');
href
中访问URL的实际操作不会发生。您可以使用
window.location
更改页面并绕过jQuery。

click()调用定义的click函数。它不模拟用户单击。下面的代码应该为您提供所需的功能

$(document).ready(function(){ // on document ready
    $("#testbed").on ('click', function() {
        alert('click event');
    });

    $("#testbed").trigger('click'); // click the element
})
.click()调用定义的click函数。它不模拟用户单击。下面的代码应该为您提供所需的功能

$(document).ready(function(){ // on document ready
    $("#testbed").on ('click', function() {
        alert('click event');
    });

    $("#testbed").trigger('click'); // click the element
})

请尝试以下代码:

$(document).ready(function(){  

//Define click function
  $("#testbed").click(function(){
     window.location.href = $("#testbed")[0].href;
  });

//Trigger the click event.
  $("#testbed").trigger('click');
});

$(document).ready(函数(){//on document ready
//下面的代码模拟单击事件
anchorObj=$(“#试验台”)[0];
var evt=新建MouseEvent('单击'{
“视图”:窗口,
“泡沫”:没错,
“可取消”:true
});
主播调度事件(evt);
});

这将起作用,因为它模拟了锚点上的实际单击事件。但是jQuery click将不起作用,因为它只执行为元素定义的click处理程序。

请尝试以下代码:

$(document).ready(function(){  

//Define click function
  $("#testbed").click(function(){
     window.location.href = $("#testbed")[0].href;
  });

//Trigger the click event.
  $("#testbed").trigger('click');
});

$(document).ready(函数(){//on document ready
//下面的代码模拟单击事件
anchorObj=$(“#试验台”)[0];
var evt=新建MouseEvent('单击'{
“视图”:窗口,
“泡沫”:没错,
“可取消”:true
});
主播调度事件(evt);
});
这将起作用,因为它模拟了锚点上的实际单击事件。但是jQuery click将不起作用,因为它将只执行为元素定义的click处理程序。

您可以试试这个

 <script>
  $(document).ready(function(){ // on document ready

   //Following code simulates a click event
    anchorObj =  $("#testbed")[0]; 
    var evt = new MouseEvent('click', {
      'view': window,
      'bubbles': true,
      'cancelable': true
    });
    anchorObj.dispatchEvent(evt);
  });
  </script>

$(document).ready(函数(){//on document ready
$(“#测试床”)[0]。单击();//单击元素
})
您可以试试这个

 <script>
  $(document).ready(function(){ // on document ready

   //Following code simulates a click event
    anchorObj =  $("#testbed")[0]; 
    var evt = new MouseEvent('click', {
      'view': window,
      'bubbles': true,
      'cancelable': true
    });
    anchorObj.dispatchEvent(evt);
  });
  </script>

$(document).ready(函数(){//on document ready
$(“#测试床”)[0]。单击();//单击元素
})

首先,您应该知道,
.trigger()
函数不能用于模拟本机浏览器事件,例如单击文件输入框或锚定标记

<script>
$(document).ready(function(){ // on document ready
  $("#testbed")[0].click(); // click the element
})
</script>
必须使用.simulate()来模拟此类本机浏览器事件

$("#testbed").trigger('click'); // it will not change the current page.
要使用
.simulate()
函数,必须在文件中包含
simulate.js

你可以参考这一页,

首先,您应该知道,
.trigger()
函数不能用于模拟本机浏览器事件,例如单击文件输入框或锚定标记

<script>
$(document).ready(function(){ // on document ready
  $("#testbed")[0].click(); // click the element
})
</script>
必须使用.simulate()来模拟此类本机浏览器事件

$("#testbed").trigger('click'); // it will not change the current page.
要使用
.simulate()
函数,必须在文件中包含
simulate.js

你可以参考这一页,

为什么不
window.location.href=$(“#测试床”).attr(“href”)window.location.href=$(“#测试床”).attr(“href”)变量url=$(“.selected”).attr(“href”)您的最终要求是什么?可能会有更好的解决方案。谷歌网站只是一个例子,我需要自动点击一个功能。如果用类选择链接更改变量url会怎么样<代码>变量url=$(“.selected”).attr(“href”)您的最终要求是什么?可能有更好的解决方案。这是您的完整代码吗?因为,如果是这样的话,您将有一个解析错误。在…的结尾