javascript - 将键盘箭头导航添加到自定义js

标签 javascript jquery wordpress navigation

我正在使用 wordpress,我的内容是这样的

<div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/2/"><img src="blah1.jpg" alt=""/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/3/"><img src="blahab.jpg" alt=""/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/4/"><img src="blahco.jpg" alt=""/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/5/"><img src="blahneat.jpg" alt=""/></a></div>

我有一个自定义 javascript,当用户单击图像时加载下一个图像。现在我想向该脚本添加左右键盘箭头导航,但我不知道如何实现它,因为我不熟悉 javascript。

$('body').on('click', '.image-wrap', function(e) { // listen for 'click' on our '.image-wrap' element
  e.preventDefault();  // Prevents default behavior on the a element

  $.ajax({

    url: $(this).find( 'a' ).attr( 'href' ), // the url we are fetching by ajax
    success: function (response) {

      newImg = $(response).find('.image-wrap').html(), // get the new href link and image from the response, contained in div.image-wrap

      $( 'div.image-wrap' ).html( newImg ); // set the new html for our inner div

    }
  }).fail(function (data) {

    if ( window.console && window.console.log ) {

      console.log( data );  // log failure to console

    }

  });

});

编辑: 通过按右箭头键,我希望它单击 image-wrap div 内的 ajax 链接,该链接应加载下一张图像。如果按向左箭头键,它应该返回到上一个图像。知道如何做到这一点吗?

最佳答案

你可以使用捕鼠器。

function GoToLocation(url)
  {
    window.location = url;
  }
  Mousetrap.bind("right", function() {
document.getElementById('next-image').click();
  });
<script src="https://craig.global.ssl.fastly.net/js/rainbow-custom.min.js?39e99"></script>
<script src="https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.js?bc893"></script>

<div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/2/"><img src="blah1.jpg" alt=""/></a></div><!--nextpage-->
<div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/3/"><img src="blahab.jpg" alt=""/></a></div><!--nextpage-->
<div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/4/"><img src="blahco.jpg" alt=""/></a></div><!--nextpage-->
<div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/5/"><img src="blahneat.jpg" alt=""/></a></div>

如果您使用基于 attachment.php 或 image.php 的图库。你也可以使用这个:Wordpress Attachment Page Navigate with Keyboard

关于javascript - 将键盘箭头导航添加到自定义js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39113351/

相关文章:

jquery - 如何更改 yajra dataTable 搜索过滤系统?

javascript - 用 JS 比较当前和给定时间

php - 使用 jQuery 和 php 获取单选按钮的值

php - Foreach 不显示结果

javascript - Knex.js Where 子句包含强制 AND 和可选 OR - SQL

javascript - 使用 jQuery,为单选按钮设置 onClick 事件监听器的最佳方法是什么?

javascript - 如何使用 Xpages onchange 事件激活按钮

javascript - 根据javascript中的键值合并两个json对象

php - 使用 SSL 加载子域时出现混合内容错误

css - 在 woocommerce 的移动 View 中显示两列产品