[英]Can we do multi actions with js & html
I have a (simple) question : is it possible to do 2 differents actions when I click on a picture on an html page?我有一个(简单的)问题:当我单击 html 页面上的图片时,是否可以执行 2 个不同的操作? Are there examples to explain that?
有例子可以解释吗?
Thanks in advance提前致谢
Sure that's easy.当然这很容易。
document.getElementById("myPic").addEventListener("click", function(e){
doActionOne();
doActionTwo();
});
You can do simultaneous actions within an onclick event attached to your picture, using jQuery or plain javaScript.您可以使用 jQuery 或普通的 javaScript 在附加到图片的 onclick 事件中同时执行操作。
In jQuery for instance, where #myImg is the id of the image:例如,在 jQuery 中,#myImg 是图像的 id:
HTML image tag: HTML 图像标记:
<img id ="myImg" alt="image" src="someImg.png">
jQuery code: https://api.jquery.com/click/ jQuery 代码: https : //api.jquery.com/click/
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
(function ($) {
$(window).load(function () {
$("#myImg").click(function () {
//Change source of the image
$(this).attr( "src", "anotherimage.png" );
// Do something else here...
});
});
})(jQuery);
</script>
Using plain javaScript:使用纯 JavaScript:
<script type="text/javascript">
window.onload = function() {
document.getElementById('myImg').addEventListener('click', function (e) {
//Change source of the image
this.setAttribute('src', 'anotherimage.png');
// Do something else here...
});
};
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.