jQuery 如何使用jQuery获取TD值。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14354682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:29:30  来源:igfitidea点击:

How to get TD value using jQuery.?

jquery

提问by Ejaz Karim

I am trying to get value of td using jquery like this.

我正在尝试使用这样的 jquery 获取 td 的值。

$("#id").find(".class").attr('value'); 
//or
$("#id").find(".class").val();

both returns empty string although it has a value. note* i'm trying get value of dynamically created element. thanks in advance.

尽管它有一个值,但两者都返回空字符串。注意* 我正在尝试获取动态创建的元素的值。提前致谢。

回答by Arvind Bhardwaj

Just write

写就好了

$("#id .class").text();

Or to get the HTML use,

或者要使用 HTML,

$("#id .class").html();

回答by Adil

The val()function is primarily used to get the values of form elements such as input, select and textarea. You need text()or html()function to get the contents of td.

VAL()函数主要用于获得形式元素,如输入,选择和textarea的值。您需要text()html()函数来获取 td 的内容。

To get text

获取文本

textOfTd = $("#id").find(".class").text();

To get Html

获取 Html

textOfTd = $("#id").find(".class").html();

回答by Sruthi Mamidala

HTML:

HTML:

<table>
    <tr>
        <td class="tdcls">1</td>
        <td class="tdcls">2</td>
        <td class="tdcls">3</td>
    </tr>
    <tr>
        <td class="tdcls">4</td>
        <td class="tdcls">5</td>
        <td class="tdcls">6</td>
    </tr>
</table>

Jquery code to select particular tdvalue:

用于选择特定td值的Jquery 代码:

$(".tdcls").mouseenter(function(){
    var a = $(this).text();
});