【问题标题】:jquery find() equivalent for javascriptjquery find() 等效于 javascript
【发布时间】:2020-10-28 17:53:56
【问题描述】:

我有三个表,我想检查它们是否包含特定元素,例如一个值为“上一个”的按钮。我通过使用 jquery 函数find 并编写了一个函数来解决它,但是我需要在没有 jquery 的情况下解决这个问题。这可能吗?

var t1 = document.getElementById("table_one");
var t2 = document.getElementById("table_two");
var t3 = document.getElementById("table_three");

has_prev_button(t1);
has_prev_button(t2);
has_prev_button(t3);

function has_prev_button(element)
{
  var has_prev_button = false;
  var check = $(element).find("input[type=button]");
  
  for (i=0; i<=check.length-1; i++) {
    if (check[i].getAttribute("value") == "Previous") {
      has_prev_button = true;
    }
  }

  if (has_prev_button) {
    document.write("<p>The selected table has a Previous button</p>");
  } else {
    document.write("<strong><p style='color:red'>The selected table has NO Previous button</p></strong>");
  }
}
table {
  margin-bottom:40px;
  border: 1px solid black;
}

td {
  border: 2px solid #D8D8D8;
  width: 70px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_one">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_two">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_three">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

【问题讨论】:

标签: javascript


【解决方案1】:

使用element.querySelectorAll

var t1 = document.getElementById("table_one");
var t2 = document.getElementById("table_two");
var t3 = document.getElementById("table_three");

has_prev_button(t1);
has_prev_button(t2);
has_prev_button(t3);

function has_prev_button(element)
{
  var has_prev_button = false;
  var check = element.querySelectorAll("input[type=button]");
  
  for (i=0; i<=check.length-1; i++)
  {
    if (check[i].value == "Previous")
    {
      has_prev_button = true;
    }
  }

  if (has_prev_button)
  {
    document.write("<p>The selected table has a Previous button</p>");
  }
  else
  {
    document.write("<strong><p style='color:red'>The selected table has NO Previous button</p></strong>");
  }
}
table {
  margin-bottom:40px;
  border: 1px solid black;
}

td {
  border: 2px solid #D8D8D8;
  width: 70px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_one">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_two">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_three">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

【讨论】:

    【解决方案2】:

    只需使用document.querySelectorAll随着类隔开.

    例如,如果我想在第三部分找到按钮并更改其背景颜色

    jQuery

    $('.my_sections').eq(2).find('.my_button').css('background', 'pink')
    

    香草JS

    document.querySelectorAll('.my_sections .my_button')[2].style.background = 'pink'
    

    同样,如果我想检查我有多少按钮my_button 类的:

    document.querySelectorAll('.my_sections .my_button').length
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 2011-08-22
      • 2020-01-01
      • 2013-05-05
      相关资源
      最近更新 更多