[英]Jquery trigger on keypress
我正在使用wordpress作為cms
我編寫了一個代碼,當我按向左鍵時嘗試運行like()函數,但我嘗試在like()函數中觸發.love,但它不起作用
這是我的代碼
var h2top = 0;
function like(){
scrollTop = jQuery(window).scrollTop();
jQuery('.container .post').each(function(i, h2){ /* loop through article headings */
h2top = jQuery(h2).offset().top ; /* get article heading top */
if (scrollTop<h2top-19) { /* compare if document is below heading */
alert("Ram");
jQuery(this).find('.love').trigger( "click" );
return false; /* exit function */
}
});
}
這是Keypress事件的jQuery代碼
jQuery(document).ready(function ($) {
$(document.documentElement).keyup(function (event) {
var direction = null;
// handle cursor keys
if (event.keyCode == 37) {
// go left
like();
$("#sad").closest('div').addClass('left');
alert("left");
} else if (event.keyCode == 39) {
// go right
alert("right");
}
});
});
僅使用alert()來檢查代碼是否正在運行
只是
$(document).keyup(function(e) {
alert (1);
// use event.which instead of keyCode
});
代替
$(document.documentElement).keyup(function (event) {
其他錯誤,在jQuery(this).find('.love').trigger( "click" );
行的like函數中jQuery(this).find('.love').trigger( "click" );
this
不是jQuery上下文,您必須像這樣使用$(this)
jQuery($(this)).find('.love').trigger( "click" );
示例(更新)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.