Javascript Jquery触发器不工作 测试触发器 $(document).ready(函数(){//on document ready $(“#测试床”).trigger('click');//单击元素 })
我希望这段代码在文档准备就绪时自动单击testbed链接。但是链接没有被点击。为什么不试试这个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
<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”)因为您已经在使用jquery了,所以使用本机javascript代码可以提高性能,我喜欢尽可能使用本机函数和属性等。您可以检查$(“#testbed”)[0]的性能差异。href和$(“#testbed”).attr(“href”);在这里,谢谢你提供的信息:)为什么不window.location.href=$(“#测试床”).attr(“href”)因为您已经在使用jquery了,所以使用本机javascript代码可以提高性能,我喜欢尽可能使用本机函数和属性等。您可以检查$(“#testbed”)[0]的性能差异。href和$(“#testbed”).attr(“href”);在这里,谢谢你提供的信息:)谷歌网站只是一个例子,我需要自动点击一个功能。如果用类选择链接更改变量url会怎么样<代码>变量url=$(“.selected”).attr(“href”)代码>您的最终要求是什么?可能会有更好的解决方案。谷歌网站只是一个例子,我需要自动点击一个功能。如果用类选择链接更改变量url会怎么样<代码>变量url=$(“.selected”).attr(“href”)代码>您的最终要求是什么?可能有更好的解决方案。这是您的完整代码吗?因为,如果是这样的话,您将有一个解析错误。在…的结尾