如何使用 jQuery 触发点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17270568/
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
How to trigger tap event with jQuery
提问by Lukas
I need to set tap
trigger on some selectors. I'm using jQuery Mobile and this function, so what is not right?
我需要tap
在一些选择器上设置触发器。我正在使用 jQuery Mobile 和这个功能,有什么不对的?
window.onload = load;
function load() {
$('.togglePlay').trigger('tap');
}
回答by suhailvs
try:
尝试:
$(document).ready(function(){
$('.togglePlay').trigger('click');
});
回答by Penny Liu
Look into tapand tapholdtouch event.
// The tap event won't be emitted along with the taphold event
$.event.special.tap.emitTapOnTaphold = false;
$("#fire").on("taphold", function() {
$(this).addClass("colorize");
});
$("#banana").on("tap", function() {
$(this).hide();
});
body {
text-align: center;
}
span {
font-size: 3rem;
}
span:hover {
cursor: pointer;
}
.colorize {
color: transparent;
text-shadow: 0 0 0 red;
}
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<h3>If you tap and hold me for 750 milliseconds, I will fill up with color.</h3>
<span id="fire"></span>
<hr />
<h3>If you tap me quick, I will dispear.</h3>
<span id="banana"></span>
回答by krishgopinath
Assuming the loading page has a div containing data-role=page
and its id
is mypage
, you could use the pageinit
event to fire up the click
on '.togglePlay'
like this :
假设加载页面有一个包含 divdata-role=page
和id
is的 div mypage
,您可以使用该pageinit
事件来启动click
on ,'.togglePlay'
如下所示:
$(document).on("pageinit", "#mypage", function () {
$('.togglePlay').trigger('click');
});
References
参考
- For reasons why you mustn't use
ready
event with jQM, see jQuery Mobile: document ready vs page events - Link for page events from jQM API : Events
- 出于不能将
ready
事件与 jQM 一起使用的原因,请参阅jQuery Mobile:文档就绪与页面事件 - 来自 jQM API 的页面事件链接:事件
回答by malthouser
you should be able to set autoplay=true or some other setting with...
您应该能够设置 autoplay=true 或其他一些设置...
<video controls autoplay>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
example from: http://www.w3schools.com/tags/att_video_autoplay.asp
示例来自:http: //www.w3schools.com/tags/att_video_autoplay.asp
without the use of js
不使用js
回答by Suryavel TR
I use jQuery tap() method.
我使用 jQuery tap() 方法。
$('.togglePlay').tap();
http://api.jquerymobile.com/tap/
http://api.jquerymobile.com/tap/
This works for me :)
这对我有用:)