简体   繁体   English

此JavaScript的错误处理

[英]Error handling for this javascript

I would like to build in error handling for this piece of javascript so that if #next doesnt exist that it will do something else. 我想为这段JavaScript构建错误处理,以便如果#next不存在,它将执行其他操作。 Currently when it doesnt exist it just gives me an undefined in the url or an error page. 当前,当它不存在时,它只是在URL或错误页面中给了我一个未定义的含义。 Please let me know what i can do here. 请让我知道我可以在这里做什么。 thanks 谢谢

<script>
$(document).on("pageinit",function(){

  var pathname = $(location).attr('href');
  var leva = $('#next').attr("href");
  var prava = $('#prev').attr("href");

  $("body").on("swiperight",function(){
    <!--alert("You swiped right!");-->
    location.href=prava;
  });                       
  $("body").on("swipeleft",function(){
    <!--alert("You swiped left!");-->
    location.href=leva;
  });
});
</script>

You can check $('#next').length to see if any elements were selected. 您可以检查$('#next').length来查看是否选择了任何元素。 Or just check if leva is undefined . 或者只是检查leva是否undefined

Something like this: 像这样:

<script>
$(document).on("pageinit",function(){

  var pathname = $(location).attr('href');
  var leva = $('#next').attr("href");
  var prava = $('#prev').attr("href");

  $("body").on("swiperight",function(){
    <!--alert("You swiped right!");-->
    location.href=prava;
  });                       
  $("body").on("swipeleft",function(){
    <!--alert("You swiped left!");-->
    if(leva) {
      location.href=leva;
    } else {
      // Some other action
    }
  });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM