访问通过JS呈现值的同级元素的值

时间:2018-10-24 03:59:10

标签: javascript jquery

这些值是通过JS放置的,我对此很执迷。我试图访问public void onBindViewHolder(MyViewHolder vh, int index) { int row = index % myArray.getHeight(); int col = index / myArray.getHeight(); if (myArray[row][col].isValid()) { img.setImageResource(R.drawable.check); } else { img.setImageResource(R.drawable.nocheck); } } 元素的值,但无济于事。如何获取值?

.id

3 个答案:

答案 0 :(得分:1)

Arrow functions

  

箭头函数表达式的语法比函数表达式短,并且没有自己的thisargumentssupernew.target。这些函数表达式最适合非方法函数,因此不能用作构造函数。

您可以使用普通的函数语法:

// click function 
$('.delete').click(function() {
  let id = $(this).next('.id').text();
  console.log(id);
})
.delete{background-color: lightgray}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tr class="tablerow">
      <td class="delete"> Delete </td>
      <td class="id">Some Text</td>
  </tr>
  <tr class="tablerow">
      <td class="delete"> Delete </td>
      <td class="id">Some Text2</td>
  </tr>
<table>

您仍然可以通过将event传递给箭头功能来使用箭头功能,以便可以使用target属性来引用当前元素:

// click function 
$('.delete').click((e) => {
  let id = $(e.target).next('.id').text();
  console.log(id);
})
.delete{background-color: lightgray}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="tablerow">
      <td class="delete"> Delete </td>
      <td class="id">Some Text</td>
  </tr>    
  <tr class="tablerow">
      <td class="delete"> Delete </td>
      <td class="id">Some Text2</td>
  </tr>
<table>

答案 1 :(得分:0)

尝试一下。

$('.delete').click(function(){

    let id = $(this).closest('tr').find('.id').text();
    console.log(id) 
 

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="tablerow">
            <td class="delete"> delete</td>
            <td class="id">1</td>
</tr>
<tr class="tablerow">
            <td class="delete">delete</td>
            <td class="id">2</td>
</tr>
<tr class="tablerow">
            <td class="delete">delete </td>
            <td class="id">3</td>
</tr>
</table>

答案 2 :(得分:-1)

如果我理解正确,则您正在尝试访问类id的表单元格。您可以这样做

$('.delete').click(() =>{
    let id = $(this).closest('td');
})