gpt4 book ai didi

javascript - this.value 在 td 值上返回 undefined

转载 作者:行者123 更新时间:2023-11-30 07:02:19 25 4
gpt4 key购买 nike

我正在尝试获取表中特定值的值。使用以下 jQuery:

$( 'td.calendar-day' ).click(function() {
console.log(this);

返回

<td class="calendar-day" value="2014-01-03">
<div class="day-number">3</div>
<div class="class"><a href="http://website.com" target="_blank">2</a></div>
<div class="class"><a href="http://website.com" target="_blank">2</a></div>
<div class="class"><a href="http://website.com" target="_blank">2</a></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
</td>

当我点击下面的单元格时

<td class="calendar-day" value="2014-01-03">

但是当我添加:

console.log(this.value);

它返回:undefined

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

使用数据-* 属性。表行不应该有值属性。最佳做法是设置数据属性然后获取它

HTML

<td class="calendar-day" data-value="2014-01-03">

Js

$('td.calendar-day').click(function() {
this.getAttribute('data-value') // compatible with all browsers and also the most performance efficient. See benchmark below
// Or .. $(this).data('value')
// Or .. this.data.value
}

Benchmark Test 由@crush 提供

关于javascript - this.value 在 td 值上返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20997607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com