javascript - 从第 n 个 td 元素中选择数据

标签 javascript jquery html css-selectors

我正在尝试从以下 html 中获取数据(“borgid”):

<tr style="cursor:default;" class="odd">
    <td class=" ">1</td>
    <td class=" ">What was your favorite childhood pet's name?</td>
    <td data-borgid="1" class=" ">Generic Hospital Networks</td>
    <td class=" ">5173</td>
    <td style="text-align:right;" class=" "><input type="button" value="Remove" name="1" class="deleteMeCQ" style="cursor:pointer;"></td>
</tr>

我尝试了 $(':nth-child(2)', $(this)).data("borgid")$tds.eq(2)。数据(“borgid”)。两者都返回“未定义”。提前致谢。

另请参阅:Is there a way to combine $(this) with :nth-child?

var a = [];
$("#ChallengeQuestionsTable tbody tr").each(function () {

var $tds = $(this).children('td');

var questionid = '';
var question = '';
var borgid = '';

if ($tds.length == 5) {
    questionid = $tds.eq(0).html();
    question = $tds.eq(1).html();
    borgid = $(':nth-child(2)', $(this)).data("Borgid");

    a.push('{ "questionid" : "' + questionid + '", "question" : "' + question + '", "borgid" : "' + borgid + '" }');
}
});

最佳答案

如果你想获得borgid,可以通过现有属性选择:

borgid = $($(this).find("[data-borgid]").get(0)).attr("data-borgid");

关于javascript - 从第 n 个 td 元素中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745589/

相关文章:

javascript - 将所有内容转换为小写后如何检查元素是否包含字符串

javascript - 将 innerHTML 传递给元素并附加事件

javascript - 正确使用 javascript 中的异常

jQuery 背景 slider 功能

javascript - 无法在 jQuery 中收听全局事件

javascript - 如何根据鼠标位置更改光标?

html - CSS 粘性 header

javascript - 如何使用jquery随机删除同一类的元素

javascript - 多维矩阵上的每元素数组运算

html - 火狐 : stop blinking cursor in div