将变量与 jQuery 选择器一起使用时出错

Error when using variable with jQuery Selector

我正在尝试获取 td 的 ID 以对它们执行一些功能。我有多个 类 但我需要这个 td.

这是我的代码:

HTML:

<table border=1 class="yo">
<tbody>
    <tr>
        <td>a</td>
        <td class="sigh">b</td>
    </tr>
</tbody>
</table>
<table border=1 class="yoyo">
<tbody>
    <tr>
        <td>x</td>
        <td class="dSigh" id="df">y</td>
    </tr>
</tbody>
</table>

jQuery:

 var abc = $('.yo').next().attr('class');
 console.log(abc);
 var tableBodyCols2 = $('.' + abc + '> .dSigh').attr('id');
 console.log(tableBodyCols2);

我得到的结果:

溜溜球

未定义

预期结果:

溜溜球

df

删除 child combinator (>) because .dSigh is not a direct child of .yoyo. You need to use the descendant combinator (space):

var abc = $('.yo').next().attr('class');
console.log(abc);
var tableBodyCols2 = $('.' + abc + ' .dSigh').attr('id');
console.log(tableBodyCols2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border=1 class="yo">
  <tbody>
    <tr>
      <td>a</td>
      <td class="sigh">b</td>
    </tr>
  </tbody>
</table>
<table border=1 class="yoyo">
  <tbody>
    <tr>
      <td>x</td>
      <td class="dSigh" id="df">y</td>
    </tr>
  </tbody>
</table>