简体   繁体   English

如何获得prev()而不只是同级

[英]How to get prev() NOT just sibling

I have 5 buttons and only 4 of the buttons are next to each other. 我有5个按钮,只有4个按钮彼此相邻。 THere is one button that is in its own div. 这是位于其自身div中的一个按钮。 How can I access it with something like prev() ? 如何使用prev()类的东西访问它?

Here is an example code: 这是示例代码:

<div>
  <div>
    <input type="button" class="test" value="Test1">
  </div>

  <input type="button" class="test" value="Test 2">
  <input type="button" class="test" value="Test 3">
  <input type="button" class="test" value="Test 4">
  <input type="button" class="test" value="Test 5">
</div>

<script>
$(".test").click(function() {
  var value = $(this).prev().val();
  alert(value);
});
</script>

Link: https://jsfiddle.net/fzwqmyb4/3/ 链接: https //jsfiddle.net/fzwqmyb4/3/

When i press "Test 4", it shows "Test 3" which is good. 当我按“测试4”时,它显示“测试3”,这很好。 But when I press on button "Test 2", it should show "Test 1" but it does not. 但是,当我按下“测试2”按钮时,它应该显示“测试1”,但没有。 The reason is because prev() only works for siblings. 原因是因为prev()仅适用于兄弟姐妹。

But how can I make a dynamic code that detects the previous of something even if its in another div? 但是,我如何制作一个动态代码来检测某事物的前一个,即使它在另一个div中呢?

You can check if the previous element is an input or not, if no, find the child. 您可以检查上一个元素是否为输入,如果不是,则查找子项。 Check these code: 检查以下代码:

$(".test").click(function(){
    var prevElement = $(this).prev().get(0);
    if(prevElement && prevElement.nodeName !== 'INPUT')
    {
      prevElement = $(prevElement).find('input').get(0);
    }

    var value = prevElement ? prevElement.value : '';
    alert(value);
});

prev() only finds the previous sibling. prev()仅查找上一个同级。 To find a nested element, you need to add some more logic to your function: 要查找嵌套元素,您需要向函数添加更多逻辑:

 $(".test").click(function() { var value; var prevTagName = $(this).prev().prop("tagName"); switch(prevTagName) { case undefined: value = "no previous siblings"; break; case "INPUT": value = $(this).prev().val(); break; case "DIV": value = $(this).closest("div").find("input").val(); break; default: value = "undefined"; break; } alert(value); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div> <input type="button" class="test" value="Test1"> </div> <input type="button" class="test" value="Test 2"> <input type="button" class="test" value="Test 3"> <input type="button" class="test" value="Test 4"> <input type="button" class="test" value="Test 5"> </div> 

  1. Using index, get the index of the current clicked element with the class test then subtract 1 to get the previous element with the same class 使用索引,使用类test获取当前单击的元素的索引,然后减去1以获取具有相同类的前一个元素

 $("input.test").click(function() { var index = $(this).index() var prevval = $("input.test").eq(index - 1).val() console.log(prevval) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div> <input type="button" class="test" value="Test1"> </div> <input type="button" class="test" value="Test 2"> <input type="button" class="test" value="Test 3"> <input type="button" class="test" value="Test 4"> <input type="button" class="test" value="Test 5"> </div> 

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

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