在 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
Getting the Span id attribute Value in Jquery
提问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 id
of the current span. if you want to assign an id
to the span you can do this.
这将获得id
当前跨度的 。如果你想为id
跨度分配一个,你可以这样做。
$("#mytable").find("td > span").attr("id", "myspan_"+i");
where i
is an incremental variable.
其中i
是增量变量。
furthermore, if you are adding id to each td > span
you 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 Value
in the element with id of #span
actually if you are looking for an id
in jquery, you can call it directly since id
is always unique.
当我阅读您的标题时,我想我想念您的问题。但我认为这就是你想要的。如果您在 jquery中寻找一个,您想获取Value
元素中 id属性的值,您可以直接调用它,因为它始终是唯一的。#span
id
id
$("#span").attr("value", "myspan_"+i);
since i
is 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 id
has an incremental myspan_1
which 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 i
since i
is the number that you want
或者你可以直接得到i
因为i
是你想要的数字
var spanid = $("#mytable").find("td > span").attr("id", "myspan_"+i);
var idvalue = i;