首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >检查下一个元素在jQuery中是否可选,如果可选,则选择它

检查下一个元素在jQuery中是否可选,如果可选,则选择它
EN

Stack Overflow用户
提问于 2015-01-02 11:23:29
回答 1查看 326关注 0票数 -3

我正在尝试创建一个简单易用的网页设计应用程序(主要用于触摸设备),甚至我的侄子都可以使用。然而,今天我遇到了一个恼人的问题。经过几个小时的反复试验,我现在请求帮助:(。

我的问题是“下一步”和“前进”箭头按钮(而不是“”)。

我所做的是,当我单击next按钮(#movenext)时,我首先查看是否有下一个元素/标记,如果有,则使用一个名为.selected的类选择该元素。如果我选择了最后一个元素,我希望得到的提示是:这是我选择的父元素的最后一个子元素。

无论我做什么,我似乎都想不出如何解决这个问题。

代码语言:javascript
复制
// Make Next Selected 
$("#movenext").click(function() {
  if ( $(".selected").is(":visible") ) {
    // Detect If New Selection Has Tag
    if ( $(".selected").next().prop("tagName") === [undefined, false] ) {
      // No Selection
      alert("last child selected");
      $("#deselectelement").trigger("click");
    } else {
      $(".selected").removeClass("selected").next().addClass("selected");
      $("#selected-tag").val( $(".selected").prop("tagName").toLowerCase() );
      $("#selected-element").val( $(".selected").attr("class").replace(/ /g," ") );
    }
  } else {
    // No element selected
    $("#moveprev, #movenext, #toparent, #tochild, #editelement, #deselectelement, #delelement, #editTagAttributes").addClass("hide");
  }
});

代码语言:javascript
复制
$(document).ready(function() {
  // Draggable FunctionBar
  $(".functionbar").draggable({
    axis: "y",
    handle: ".handlesbar"
  });

  // Select Elements
  var SelectElements = function() {
    $("#dynamic-storage").children().on("mouseup touchend", function() {
      if ( $(".selected").is(":visible") ) {
        $(".selected").removeClass("selected");
      }

      $(this).addClass("selected");

      // Check if first & last child is selected
      if ( $(this).is(":first-child") && $(this).is(":last-child") ) {
        if ( $("#moveprev").hasClass() === "hide" ) {
          // Do nothing
        } else if ( $("#movenext").hasClass() === "hide" ) {
          // Do nothing
        } else {
          $("#moveprev, #movenext").addClass("hide");
          $("#tochild").removeClass("hide");
          return false;
        }
        // Check if first child is selected
      } else if ( $(this).is(":first-child") ) {
        if ( $("#moveprev").hasClass() === "hide" ) {
          // Do nothing
        } else {
          $("#moveprev").addClass("hide");
          $("#movenext, #tochild").removeClass("hide");
          return false;
        }
        // Check if last child is selected
      } else if ( $(this).is(":last-child") ) {
        if ( $("#movenext").hasClass() === "hide" ) {
          // Do nothing
        } else {
          $("#movenext").addClass("hide");
        }
        $("#moveprev, #tochild").removeClass("hide");
      } else {
        // Check if in middle
        $("#moveprev, #movenext, #tochild").removeClass("hide");
      }
    });
  };
  // Call Selection
  var CallSelection = function() {
    // Check if first & last child is selected
    if ( $(".selected").is(":first-child") && $(".selected").is(":last-child") ) {
      if ( $("#moveprev").hasClass() === "hide" ) {
        // Do nothing
      } else if ( $("#movenext").hasClass() === "hide" ) {
        // Do nothing
      } else {
        $("#moveprev, #movenext").addClass("hide");
        $("#tochild").removeClass("hide");
        return false;
      }
      // Check if first child is selected
    } else if ( $(".selected").is(":first-child") ) {
      if ( $("#moveprev").hasClass() === "hide" ) {
        // Do nothing
      } else {
        $("#moveprev").addClass("hide");
        $("#movenext, #tochild").removeClass("hide");
        return false;
      }
      // Check if last child is selected
    } else if ( $(".selected").is(":last-child") ) {
      if ( $("#movenext").hasClass() === "hide" ) {
        // Do nothing
      } else {
        $("#movenext").addClass("hide");
      }
      $("#moveprev, #tochild").removeClass("hide");
    } else {
      // Check if in middle
      $("#moveprev, #movenext, #tochild").removeClass("hide");
    }
  };
  // Clear Selection
  var ClearSelection = function() {
    $(".selected").removeClass("selected");
    $("#moveprev, #movenext, #toparent, #tochild").addClass("hide");
  };
  SelectElements();
});
代码语言:javascript
复制
/* Body */
#dynamic-storage {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 0;
}

/* FunctionBar */
.functionbar {
  position: absolute;
  top: 20px;
  left: 25px;
  right: 25px;
  z-index: 998;
}

.functionbar .handlesbar {
  position: absolute;
  top: 0;
  left: 0;
  margin: auto;
  width: 100%;
  padding: 6px 0;
  border-radius: 10px 10px 0 0;
  font: 24px arial;
  text-align: center;
  background: hsla(180, 0%, 0%, .75);
  word-spacing: 12px;
  z-index: 999;
  -webkit-user-select: none;
   -khtml-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
       -o-user-select: none;
          user-select: none;
}

.functionbar a {
  text-decoration: none;
  color: hsl(180, 0%, 90%);
}

.functionbar .active {
  color: #9cf;
}

.functionbar .hide {
  display: none;
}

/* Dialog Sec */
.functionbar .reveal {
  position: relative;
  top: 50px;
  left: 0;
  margin: auto;
  width: 100%;
  height: 200px;
  overflow: auto;
}

.functionbar .reveal, .functionbar input[type=text], .functionbar textarea {
  font-family: arial;
  background: rgba(255, 255, 255, 0.6);
}

.functionbar input[type=text], .functionbar textarea {
  border: 1px solid rgba(0, 0, 0, 0.6);
  margin-bottom: 10px;
}

.functionbar input[type=text], .functionbar textarea, #change-selected-tag, #confirm-style {
  font-size: 24px;
  color: #000;
}

#dynamic-storage .selected {
  outline: 2px dotted #69f;
}

/* Addable Elements */
.functionbar .addcontent {
  text-align: center;
}

.functionbar .addcontent .element, .functionbar .editcontent .element {
  cursor: pointer;
  display: inline-block;
  text-align: center;
  padding: 2%;
  width: 20%;
  border: 1px solid rgba(204, 204, 204, 0.67);
  color: #262B2F;
}
.functionbar .element:hover {
  background: rgba(255, 255, 255, 0.6);
}
.functionbar .element .fa {
  font-size: 26px;
}
.functionbar .element h4 {
  font-weight: 300;
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=9" />
    <link type='text/css' rel='stylesheet' href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css" />
    <link type='text/css' rel='stylesheet' href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
    <link type="text/css" rel="stylesheet" href="assets/fonts/font-awesome.min.css" />
  </head>
  <body>
    <!-- FunctionBar -->
    <div class="functionbar">
      
      <div class="handlesbar">
        <div class="handlesbar-page1">
          <a id="moveprev" title="Select Previous Element" href="javascript:void(0)">
            <span class="fa fa-angle-left"></span>
          </a> 
          <a id="movenext" title="Select Next Element" href="javascript:void(0)">
            <span class="fa fa-angle-right"></span>
          </a> 
          <a id="toparent" class="hide" title="Select Parent" href="javascript:void(0)">
            <span class="fa fa-caret-square-o-up"></span>
          </a> 
          <a id="tochild" title="Select First Index" href="javascript:void(0)">
            <span class="fa fa-caret-square-o-down"></span>
          </a> 
        </div>
      </div>
    </div>
    
    <div id="dynamic-storage">
      <div class="header" align="center">
        <h1>Welcome</h1>
        <h5>My name is Michael.</h5>
        <span>Hello world</span>
      </div>
      <div class="header selected" align="left">
        <h1>Welcome</h1>
        <h5>My name is Michael.</h5>
      </div>
      <div class="header" align="right">
        <h1>Welcome</h1>
        <h5>My name is Michael.</h5>
      </div>
    </div>
  </body>
</html>

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27737028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档