在 Jquery 中获取 Span id 属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3331580/
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 15:15:23  来源:igfitidea点击:

Getting the Span id attribute Value in Jquery

jqueryjquery-selectors

提问by Someone

I have a "mytable" which contains "mytd" inturn contains the "Myspan" i want to get the .I want to get the "myspan_1" in jquery .How do i do that.

我有一个包含“mytd”的“mytable”反过来包含我想要获取的“Myspan”。我想在 jquery 中获取“myspan_1”。我该怎么做。

Small try here

小尝试这里

$("#mytable").find('td > #span').attr("value","myspan_+i");

where "i" is an incrementing value

其中“i”是一个递增的值

I coud not get Span id .All iam trying "id" value of span tag

我无法获得 Span id 。我正在尝试 span 标签的“id”值

回答by rob waminal

try these

试试这些

$("#mytable").find("td > span").attr("id");

this will get the idof the current span. if you want to assign an idto the span you can do this.

这将获得id当前跨度的 。如果你想为id跨度分配一个,你可以这样做。

$("#mytable").find("td > span").attr("id", "myspan_"+i");

where iis an incremental variable.

其中i是增量变量。

furthermore, if you are adding id to each td > spanyou can do a for loop or $.each()in all td of #mytable

此外,如果您要为每个添加 id,则td > span可以执行 for 循环或$.each()在所有 td 中#mytable

var i=0;
$.each($("#mytable").find("td"), function(){
    $(this).find("span").attr("id", "myspan_"+i);
    i++;
});

Edit:

编辑:

As I read your title i think I miss understood your question. But I think here's what you want. You want to get the value of the attribute Valuein the element with id of #spanactually if you are looking for an idin jquery, you can call it directly since idis always unique.

当我阅读您的标题时,我想我想念您的问题。但我认为这就是你想要的。如果您在 jquery中寻找一个,您想获取Value元素中 id属性的值,您可以直接调用它,因为它始终是唯一的。#spanidid

$("#span").attr("value", "myspan_"+i);

since iis an incremental value, you need to put it outside the quotes.

由于i是增量值,因此您需要将其放在引号之外。

Edit 2:

编辑2:

To add value to an attribute value

为属性添加值 value

$("#mytable").find("td > #span").attr("value", "myspan_"+i);

Edit 3:

编辑3:

Assuming your span idhas an incremental myspan_1which always starts at 1

假设您span id有一个myspan_1始终从 1 开始的增量

var spanid = $("#mytable").find("td > span").attr("id", "myspan_"+i);
var idvalue = spanid.split('_')[1];

this will get the 1 of the myspan_1

这将获得 myspan_1 的 1

or you could just directly get the isince iis the number that you want

或者你可以直接得到i因为i是你想要的数字

var spanid = $("#mytable").find("td > span").attr("id", "myspan_"+i);
var idvalue = i;